jess3333 Posted June 16, 2006 Share Posted June 16, 2006 Hi All, I am just learning PHP and need help. I realize this may have been answered already, but I wasn't sure what to search for so I thought I should just post a new question. I found a tutorial processing forms (on another website) and copied the code (see below) because I need to do something similar for the website I am building. When I tried it out, it didn't work. (I tried to contact the person who wrote the tutorial but their email is not valid.)The problem is when I fill in the form and press the submit button, it is supposed to execute the "if ($submit)" statement, but it doesn't. The form just resets and the database does not get updated. Would you have any idea why it does not work? I am sure there are better ways of processing a form and I am open to suggestions but this is bugging me because I can't figure out why it won't work. Thanks for your time and help, it is very much appreciated. Cheers, Jess<?phpif ($submit) { // process form $db = mysql_connect("localhost", "root"); mysql_select_db("mydb",$db); $sql = "INSERT INTO employees (first,last,address) VALUES ('$first','$last','$address')"; $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> <input type="Submit" name="submit" value="Enter information"> </form> <?php} // end if?> Link to comment https://forums.phpfreaks.com/topic/12180-new-to-php-and-need-help/ Share on other sites More sharing options...
dptr1988 Posted June 16, 2006 Share Posted June 16, 2006 It looks like that PHP code was depending on 'register_globals' to be on. Try using $_POST['var_name'] instead of $var_name Link to comment https://forums.phpfreaks.com/topic/12180-new-to-php-and-need-help/#findComment-46413 Share on other sites More sharing options...
wildteen88 Posted June 16, 2006 Share Posted June 16, 2006 The person that did the tutorial may had resgister_globals turned on and your php setup has it off, which a good thing.This code should now work:[code]<?phpif (isset($_POST['submit'])){ // process form $db = mysql_connect("localhost", "root", ""); mysql_select_db("mydb", $db); // make data safe before entering it into the database $first = mysql_real_escape_string($_POST['first']); $last = mysql_real_escape_string($_POST['last']); $address = mysql_real_escape_string($_POST['address']); $sql = "INSERT INTO employees (first,last,address) VALUES ('$first','$last','$address')"; $result = mysql_query($sql); echo "Thank you! Information entered.\n";}else{ //display the 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> <input type="Submit" name="submit" value="Enter information"></form><?php} // end if?>[/code] Link to comment https://forums.phpfreaks.com/topic/12180-new-to-php-and-need-help/#findComment-46414 Share on other sites More sharing options...
jess3333 Posted June 16, 2006 Author Share Posted June 16, 2006 Thanks for your suggestion. I will try it out. [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] Jess Link to comment https://forums.phpfreaks.com/topic/12180-new-to-php-and-need-help/#findComment-46416 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.