Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

simple insert help needed

Codes here !

Moderators: macek, egami, gesf

simple insert help needed

Postby tranquillo » Sun Jan 05, 2003 6:10 pm

Hi.. I'm kinda new to PHP..
I've been looking at a place on the net that had a few lessons in basic php/mysql but nothing there worked. I mean, some stuff worked, but not the stuff I wanted.

I just simply want to be able to insert data into a database through a simple form...


this is what I get from what they told me:
-------------------------------------------------------------
<?php

if ($submit) {

$db = mysql_connect("localhost", "root");

mysql_select_db("mydb",$db);

$sql = "INSERT INTO employees (first,last,address,position) VALUES ('$first','$last','$address','$position')";

$result = mysql_query($sql);

echo "Thank you! Information entered.\n";

} else{

// display form

?>

<form method="post" action="<?php echo $PHP_SELF?>">

First name:<input type="Text" name="first"><br>

Last name:<input type="Text" name="last"><br>

Address:<input type="Text" name="address"><br>

Position:<input type="Text" name="position"><br>

<input type="Submit" name="submit" value="Enter information">

</form>

<?php

} // end if

?>
-------------------------------------------------------------
what's wrong with it?
and if nothings wrong with the code, what else could be messed up?
what happens is that the form shows, I ender data into the fields and press submit and the fields go blanc.
the "thank you" message don't show and when I check the database nothing has been entered.

I'd be glad for any help at all...

thanks
tranquillo
New php-forum User
New php-forum User
 
Posts: 74
Joined: Sun Nov 24, 2002 6:48 pm
Location: katrineholm

Postby Joan Garnet » Mon Jan 06, 2003 11:00 am

Well, It could be the variables you are passing.
They are not written in the "new" way.
Some time ago, it would had worked, but now you have to refer to GET or POST vars using
$HTTP_GET_VARS["my_var_name"]
(when you pass them using GET)
$HTTP_POST_VARS["my_var_name"]
(when you pass them using POST)

So you script should work if you do the following -->

Code: Select all
if ($HTTP_POST_VARS["submit"]) {

$db = mysql_connect("localhost", "root");

mysql_select_db("mydb",$db);

$sql = "INSERT INTO employees (first,last,address,position) VALUES ('$HTTP_POST_VARS["first"]','$HTTP_POST_VARS["last"]','$HTTP_POST_VARS["address"]','$HTTP_POST_VARS["position"]')";

$result = mysql_query($sql);

echo "Thank you! Information entered.\n";

} else{

// display form


bye
;)
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron