Jump to content

$_REQUEST[ ] works right, but is not running the query


Go to solution Solved by kicken,

Recommended Posts

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

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:

 

kQUY4cg.png

  • Solution

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.

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! :P

Also, don't repeat the $password variable twice in your if statement.

 

rofl I noticed that in my source after I posted this :P 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 :P

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.