Jump to content

Variable is not working in mySQL query


AcidDriver

Recommended Posts

I have assigned a POST to a variable, yet it is not being added to the mySQL query in my PHP code.  I have the variable echo back if the POST works, and it does, but the query does not work when I add the variable.

 

PHP CODE

 

$donor=$_POST["donor"];

echo $donor;

 

mysql_query("INSERT INTO `hope` (fname, lname, idnum, gradyear, donor) VALUES ('$_POST[fname]','$_POST[lname]','$_POST[idnum]','$_POST[gradyear]','$donor')");

 

 

HTML Code for the donor

 

Donor: <br>

<dd>Yes: <input type="radio" name="donor" value="y"></dd><br>

<dd>No: <input type="radio" name="donor" value="n"></dd><br><br>

 

 

 

Whenever I remove the donor field and the variable from the query, it works perfectly.  here is the output when the query has the variable.'

 

y

 

Information Received!

 

Input another volunteer

 

 

 

The y is for a radio button being passed y.

 

Link to comment
Share on other sites

 

PHP CODE

 

$donor=$_POST["donor"];

echo $donor;

 

mysql_query("INSERT INTO `hope` (fname, lname, idnum, gradyear, donor) VALUES ('$_POST[fname]','$_POST[lname]','$_POST[idnum]','$_POST[gradyear]','$donor')");

 

 

Try putting the $_POST outside of the quotes, and you need to have either single or double quotes to access the $_POST variables

mysql_query("INSERT INTO `hope` (fname, lname, idnum, gradyear, donor) VALUES ('".$_POST['fname']."','".$_POST['lname']."','".$_POST['idnum']."','".$_POST['gradyear']."','$donor')");

 

Link to comment
Share on other sites

The $_Post works fine, that is not the problem.

 

it is the donor variable. i cant get it to work, i will try your method of the $_POST with the donor.

 

EDIT

 

The same thing happens, where info is not published to the mySQL database.  NO effect, but I will use this method from now on

Link to comment
Share on other sites

There's no reason why your original code shouldn't work, syntactically.  If your query works without the column/value then there's something wrong with the query string itself that sql doesn't like. A good way to figure out if your query string is correct is to separate the string from the query so you can echo out what's being sent to the db.  Also, adding some error reporting is helpful:

 

$sql = "INSERT INTO `hope` (fname, lname, idnum, gradyear, donor) VALUES ('$_POST[fname]','$_POST[lname]','$_POST[idnum]','$_POST[gradyear]','$donor')";
mysql_query($sql) or die("$sql <br />" . mysql_error());

Link to comment
Share on other sites

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.