PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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:
<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.
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.