Page 1 of 1
n00b question
Posted: Thu Feb 26, 2004 9:03 am
by lyte
I'm trying to setup my first mysql database and insert data into it using forms. I've been looking through a tutorial and trying to use code from it to test my dbase. everything seems to work, I can pull data from it but when I try to add data to it using forms and a button, it just seems to refresh the page and not insert the data. The code I have been using is as follows:
Code: Select all
<html>
<body>
<?php
if ($submit) {
// process form
$db = mysql_connect("localhost", "root", "password");
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
?>
</body>
</html>
Now what happens is that, I fill out the forms and click the submit button and the page just seems to refresh, no error message just a reload of the page and the data does not get inserted but it clears the data from the textboxes. I noticed that in the "if then statement that if i took out the $ from $submit that it would show me: "Thank you! Information entered." and it inserts blank data into the database. What could I be missing? It just seems like the varible $submit is not being set = true when I click the button. The mysql_connect is set correctly on my webpage. Thanks for any help you can provide.
LyTe
Posted: Thu Feb 26, 2004 9:07 am
by twigletmac
Unfortunately the tutorial you are following is a bit outdated and relies on functionality that is no longer available by default in PHP.
You need to change:
Code: Select all
<form method="post" action="<?php echo $PHP_SELF?>">
to
Code: Select all
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
and
to
and
Code: Select all
$sql = "INSERT INTO employees (first,last,address,position) VALUES ('$first','$last','$address','$position')";
to
Code: Select all
$sql = "INSERT INTO employees (first,last,address,position) VALUES ('{$_POST['first']}','{$_POST['last']}','{$_POST['address']}','{$_POST['position']}')";
Mac
Posted: Thu Feb 26, 2004 1:26 pm
by lyte
I implemented the requested changes and now I get a: Parse error: parse error, unexpected T_STRING in /var/www/html/test.php on line 10
which refers to the $db = mysql_connect("localhost","root","password"); line. I have the proper hostname, username and password in there. Any other help would be greatly appriciated.
Code: Select all
<html>
<body>
<?php
if (isset($_POSTї'submit'])) {
// process form
$db = mysql_connect("localhost","root","password");
mysql_select_db("mydb",$db);
$sql = "INSERT INTO employees (first,last,address,position) VALUES ('{$_POSTї'first']}','{$_POSTї'last']}','{$_POSTї'address']}','{$_POSTї'position']}')";
$result = mysql_query($sql);
echo "Thank you! Information entered.\n";
} else{
// display form
?>
<form method="post" action="<?php echo $_SERVERї'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>
</body>
</html>
Posted: Thu Feb 26, 2004 1:43 pm
by malcolmboston
you shoud think about simplifying your $_POST['variables']; by turning them into something else
for eg
this will make your queries look much nicer, and therefore easier to debug
Posted: Thu Feb 26, 2004 3:05 pm
by McGruff
String problem, I think. Try concatenating.
Code: Select all
<?php
$sql = "INSERT INTO employees (first,last,address,position)
VALUES ('" . $_POST['first'] . "','" . $_POST['last'] . "','" . $_POST['address'] . "','" . $_POST['position'] . "')";
?>
Posted: Thu Feb 26, 2004 4:15 pm
by lyte
Thanks but its still no good.
Code: Select all
<html>
<body>
<?php
if (isset($_POSTї'submit'])) {
// process form
$db = mysql_connect("localhost","root","password");
mysql_select_db("mydb",$db);
$sql = "INSERT INTO employees (first,last,address,position)
VALUES ('" . $_POSTї'first'] . "','" . $_POSTї'last'] . "','" . $_POSTї'address'] . "','" . $_POSTї'position'] . "')";
$result = mysql_query($sql);
echo "Thank you! Information entered.\n";
} else{
// display form
?>
<form method="post" action="<?php echo $_SERVERї'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>
</body>
</html>
Posted: Thu Feb 26, 2004 4:18 pm
by infolock
Try using this :
Code: Select all
<html>
<body>
<form method="post" action="<?php echo $_SERVER['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>
</body>
</html>
<?php
if (isset($_POST['submit'])) {
// process form
mysql_connect("localhost","root","password") or die(MySQL_Error());
mysql_select_db("mydb") or die(MySQL_Error());
$sql = "INSERT INTO employees (first,last,address,position) VALUES ('".$_POST['first']."','".$_POST['last']."','".$_POST['address']."','".$_POST['position']."')";
$result = mysql_query($sql) or die(MySQL_Error());
echo "Thank you! Information entered.\n";
} else{
// display form
?>
and see if you are getting any errors.. (notice the die messages)
(edit : you also missed a ?> in the <form method="post" action="<?php echo $_SERVER['PHP_SELF'];"> . so i added that too