On Tuesday, March 19, 2002, at 10:57  AM, Denis L. Menezes wrote:

> I have three fields(input boxes) in a php form as follows :
> $name
> $rank
> $age
>
> My MySQL table is called "tblAddress". I know how to connect, but I do 
> not
> know the "Insert" statement. Can someone please advise me how I can 
> insert
> the above values in the tables "tblAdddress" in columns "name", "Rank",
> "age" respectively?

There are a couple of ways to do it.

$sql = "INSERT INTO tblAddress
         VALUES (" . $_POST['name'] . ", " . $_POST['rank'] . ", " . 
$_POST['age'] . ")";
mysql_query($sql, $db);  // $db is your database connection pointer

That one only works if the first three columns in the table are the ones 
you want to insert the values into.  The next version is like this:

$sql = "INSERT INTO tblAddress (name, rank, age)
         VALUES ('" . $_POST['name'] . "', '" . $_POST['rank'] . "', '" . 
$_POST['age'] . "')";
mysql_query($sql, $db);  // $db is your database connection pointer

That one works if you don't know the layout of the table, but know the 
names of the columns you want -- it's specific, and more portable than 
the first way (in case you ever alter your table structure).  The last 
way to do it is like this:

$sql = "INSERT INTO tblAddress
         SET name='" . $_POST['name'] . "'
             rank='" . $_POST['rank'] . "'
             age='" . $_POST['age'] . "'";
mysql_query($sql, $db);  // $db is your database connection pointer


In the above examples, I've made the assumption that the form is being 
sent via POST and that you are using PHP 4.1.x with register_globals 
off, adjust accordingly if this is not the case.  Also note that the 
singlequotes used to surround each database entry value aren't always 
necessary, I think that with INT columns they can be omitted (but are 
required for string columns since spaces are tokens in this case [I 
think]).

HTH,

Erik




----

Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to