Jump to content

Recommended Posts

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

<?php

if ($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

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]<?php

if (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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.