Jump to content

Archived

This topic is now archived and is closed to further replies.

jess3333

New to PHP and need help

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

?>

Share this post


Link to post
Share on other sites
It looks like that PHP code was depending on 'register_globals' to be on. Try using $_POST['var_name'] instead of $var_name

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.