jParnell Posted July 27, 2013 Share Posted July 27, 2013 Bear with me, I'm new to PHP and MySQL. Ok, so strange thing. Last night, I'm coding it up, everything's working great. Go to bed, wake up, now my form isn't working. Literally 9 hours before, it was. Nothing changed, suddenly isn't working. I have 2 files, form.php and insert.php. form.php: <html> <head> <title>YAY!</title> </head> <body> <h2>Register Form</h2> <form method="get" action="insert.php"> <table border="0" width="60%"> <tr> <td width="10%">Referred By: </td><td><input type="text" name="referredby" /></td> </tr> <tr> <td width="10%">Email: </td><td><input type="text" name="email" /></td> </tr> <tr> <td width="10%">Name: </td><td><input type="text" name="name" /></td> </tr> <tr> <td width="10%">Password: </td><td><input type="text" name="password" /></td> </tr> <tr> <td width="10%">Earned: </td><td><input type="text" name="earned" /></td> </tr> <tr> <td width="10%">Image: </td><td><input type="text" name="image" /></td> </tr> </table> <p> <input type="submit" value="Register" /> <input type="reset" value="reset" /> </form> </body> </html> insert.php: <?php $referredby = $_REQUEST['referredby']; $email = $_REQUEST['email']; $name = $_REQUEST['name']; $password = $_REQUEST['password']; $earned = $_REQUEST['earned']; $image = $_REQUEST['image']; if($email && $name && $password && $password && $earned) { mysql_connect("dbserver", "username", "password") or die("Problem connecting to the database server. Please contact administrator at <link to contact form here> with the following error message: " . mysql_errno() . ": " . mysql_error() . "<br />" ); mysql_select_db("database"); mysql_query("INSERT INTO desiredtable(referredby,email,name,password,earned,image) VALUES('$referredby','$email','$name','$password','$earned','$image')"); $registered = mysql_affected_rows(); echo "$name was successfully registered! $registered row(s) added to the table."; mysql_close(); }else{ echo "You have to complete the form<br />"; echo "Email: " . $email . "<br />" . "Name: " . $name . "<br />" . "password: " . $password . "<br />" . "Earned: " . $earned . "<br />"; } echo mysql_errno() . ": " . mysql_error() . "<br />"; echo "<a href=\"form.php\">Return to the previous page</a>" ?> Now, here's the kicker. When I fill out the form and click register, I have my else { } statement echo the $_REQUEST[ ] variables (added that for troubleshooting), and it's displaying the contents correctly. I even changed my form from method="post" to method="get" to verify in the address bar. Apparently, everything before and after the initial IF statement conditions are working correctly. It's the conditions that are not, which doesn't make sense, because as I said, I was using it last night in order to add entries into my table. It's probably something really simple stupid, and I'm hoping you can help me out with it :/ FYI, this is set up under a XAMPP installation on my PC Quote Link to comment Share on other sites More sharing options...
jParnell Posted July 27, 2013 Author Share Posted July 27, 2013 Just found something interesting....... when filling out the form, on the input marked EARNED, if I set it to 0, it will not run. If I set it to 1, it runs successfully. Last night I was setting it to 0 all night long for about 20 different dummy test users. Now, nothing. Here's how my table is set up: Quote Link to comment Share on other sites More sharing options...
Solution kicken Posted July 27, 2013 Solution Share Posted July 27, 2013 if ($email && $name && $password && $password && $earned) That tests that each of those values has a truthy value, ie something that is considered to be true if converted to a boolean value. "0" is considered to be false if converted to a boolean value, hence your test fails when you enter 0 for earned. If you want to check if the values were posted, you should be using isset on the original $_REQUEST variables. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted July 27, 2013 Share Posted July 27, 2013 Also, don't repeat the $password variable twice in your if statement. Quote Link to comment Share on other sites More sharing options...
jParnell Posted July 27, 2013 Author Share Posted July 27, 2013 if ($email && $name && $password && $password && $earned) That tests that each of those values has a truthy value, ie something that is considered to be true if converted to a boolean value. "0" is considered to be false if converted to a boolean value, hence your test fails when you enter 0 for earned. If you want to check if the values were posted, you should be using isset on the original $_REQUEST variables. Brilliant! I changed the original lines of: <?php $referredby = $_REQUEST['referredby']; $email = $_REQUEST['email']; $name = $_REQUEST['name']; $password = $_REQUEST['password']; $earned = $_REQUEST['earned']; $image = $_REQUEST['image']; if($email && $name && $password && $password && $earned) { mysql_connect("dbserver", "username", "password") or die To: <?php if(isset($_REQUEST['email']) && isset($_REQUEST['name']) && isset($_REQUEST['password']) && isset($_REQUEST['earned'])) { $referredby = $_REQUEST['referredby']; $email = $_REQUEST['email']; $name = $_REQUEST['name']; $password = $_REQUEST['password']; $earned = $_REQUEST['earned']; $image = $_REQUEST['image']; mysql_connect("dbserver", "username", "password") or die( Now works brilliantly! I just wonder why it worked last night and didn't today? I swear, I changed absolutely NOTHING! Quote Link to comment Share on other sites More sharing options...
jParnell Posted July 27, 2013 Author Share Posted July 27, 2013 Also, don't repeat the $password variable twice in your if statement. rofl I noticed that in my source after I posted this it wasn't popping any error messages, or I would have noticed it sooner. I had copied the script from another site I had where it had $password and $vpassword, and I guess I removed the v or something... i dont know, it was 4 am this morning when I wrote this, 22 hour days tend to lead to fuzzy code Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.