AcidDriver Posted July 23, 2008 Share Posted July 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/116215-variable-is-not-working-in-mysql-query/ Share on other sites More sharing options...
adam84 Posted July 23, 2008 Share Posted July 23, 2008 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')"); Quote Link to comment https://forums.phpfreaks.com/topic/116215-variable-is-not-working-in-mysql-query/#findComment-597611 Share on other sites More sharing options...
AcidDriver Posted July 23, 2008 Author Share Posted July 23, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/116215-variable-is-not-working-in-mysql-query/#findComment-597612 Share on other sites More sharing options...
revraz Posted July 23, 2008 Share Posted July 23, 2008 donor in your radio button should be an array since you use the name twice, like a checkbox. Quote Link to comment https://forums.phpfreaks.com/topic/116215-variable-is-not-working-in-mysql-query/#findComment-597619 Share on other sites More sharing options...
AcidDriver Posted July 23, 2008 Author Share Posted July 23, 2008 But I only want there to be one output, either y or n. wouldnt an array be for something when i want more than 1 output. like 2 boxes checked. Quote Link to comment https://forums.phpfreaks.com/topic/116215-variable-is-not-working-in-mysql-query/#findComment-597657 Share on other sites More sharing options...
.josh Posted July 23, 2008 Share Posted July 23, 2008 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()); Quote Link to comment https://forums.phpfreaks.com/topic/116215-variable-is-not-working-in-mysql-query/#findComment-597665 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.