p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: linking forms to mysql databases


Message #1 by simonhenderso1@h... on Mon, 16 Jul 2001 13:53:18
i'm new to php and am having a problem with linking form text boxes to 

mysql database tables.



i want to link the entry in a form text box to the relevant field in the 

database table for a user registration scenario.



can anyone help?
Message #2 by "Richard A. Holmes(personal)" <holmesra@w...> on Mon, 16 Jul 2001 12:01:03 -0600
First name the input box what ever field name you are using then in the

script , use that name and add the $ to the front of it



-----Original Message-----

From: simonhenderso1@h... [mailto:simonhenderso1@h...]

Sent: Monday, July 16, 2001 7:53 AM

To: beginning php

Subject: [beginning_php] linking forms to mysql databases





i'm new to php and am having a problem with linking form text boxes to 

mysql database tables.



i want to link the entry in a form text box to the relevant field in the 

database table for a user registration scenario.



can anyone help?





Message #3 by Jorge Cordero <jorge@d...> on Mon, 16 Jul 2001 14:16:58 -0600
this is basic stuff

first you need to understand that everything that you put inside an iput

field is a variable so as the other persons toldyou you need to name the

input fiel exaclty the same name as per your db,

<input type="text" name="myfield"> now whatr ever the user inputs in there

it will be named as "myfield" once the user clicks the submit button the

info is passed as the variable "$myfield" therefore you need to reffer to

this info as $myfiled.

$qinsert=mysql_query("INSERT  INTO mytable (myfield)VALUES('$myfield');

this should get you going



Jorge Cordero



-----Mensaje original-----

De: Richard A. Holmes(personal) <holmesra@w...>

Para: beginning php <beginning_php@p...>

Fecha: Lunes, 16 de Julio de 2001 12:06 p.m.

Asunto: [beginning_php] RE: linking forms to mysql databases





>First name the input box what ever field name you are using then in the

>script , use that name and add the $ to the front of it

>

>-----Original Message-----

>From: simonhenderso1@h... [mailto:simonhenderso1@h...]

>Sent: Monday, July 16, 2001 7:53 AM

>To: beginning php

>Subject: [beginning_php] linking forms to mysql databases

>

>

>i'm new to php and am having a problem with linking form text boxes to

>mysql database tables.

>

>i want to link the entry in a form text box to the relevant field in the

>database table for a user registration scenario.

>

>can anyone help?

>

>



Message #4 by "Hermawan Haryanto" <hermawan@h...> on Wed, 25 Jul 2001 21:31:11
Hi all, I'm a new guy in here, I want to make an answer to this question.

I'm so sorry for my bad english.

First of all you need to create mysql database table (you can do it with 

mr. tobias's phpMyAdmin) then use the same fields for all fields on the 

forms (it's easier)



Form Example :

<input type="textfield" name="firstname"><br>

<input type="textfield" name="lastname"><br>

<input type="textfield" name="username"><br>

<input type="textfield" name="heroname"><br>

<input type="submit" name="action" value="submit">



Database Table Example :

id => Int, Not Null, Auto Incriement

firstname => Varchar 50

lastname => Varchar 50

username => Varchar 16

heroname => Varchar 50



Form Processor Example :



<?php

  if(trim($action)=="submit"){

    $db_name="your database name"; //Your database

    $db_user="your database user"; //Your database username

    $db_pass="your database password"; //Your database password

    $db_host="your database hostname"; //Your database hostname, usually 

using Localhost if you have the same database server with the apache/php 

server

    $conn=mysql_connect($db_host,$db_user,$db_pass) OR DIE ("Hei, the 

server is dead?");

    @mysql_select_db($db_name,$conn);

    $forms = $HTTP_POST_VARS;

    foreach($forms as $key=>$value){

      if($key!="action"&&trim($value)!=""){

        $fields.=$key.",";

        $values.="'".trim($value)."',"

      }

    }

    $fields=substr($fields,0,strlen($fields)-1);

    $values=substr($values,0,strlen($values)-1);

    $sql="INSERT INTO yourtablename ($fields) VALUES ($values)";

    @mysql_query($sql,$conn)

  }

?>



Hope that helps



Thanks

Hermawan Haryanto <hermawan@h...>





> i'm new to php and am having a problem with linking form text boxes to 

> mysql database tables.

> 

> i want to link the entry in a form text box to the relevant field in the 

> database table for a user registration scenario.

> 

> can anyone help?

  Return to Index