Jump to content


Photo

New to PHP and need help


  • Please log in to reply
3 replies to this topic

#1 jess3333

jess3333
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 16 June 2006 - 06:42 PM

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

?>



#2 dptr1988

dptr1988
  • Members
  • PipPipPip
  • Advanced Member
  • 372 posts

Posted 16 June 2006 - 06:55 PM

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

Need more help with your project? One of the thousands of programmers, web designers or artists at <a href="http://www.rentacode...d_6764522">Rent A Coder</a> would be happy to help.

Disclaimer: Free advice is usually worth what you paid for it. ( or at least when it's coming from me! )

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 16 June 2006 - 06:55 PM

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:
<?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
?>


#4 jess3333

jess3333
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 16 June 2006 - 06:59 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users