mister_bluesman Posted July 3, 2009 Share Posted July 3, 2009 Hi. I have a form that contains a checkbox. <form action="add_info_handler.php" method="post" > <table> <tr> <td align="right">Add Info</td> <td align="left"><input type="checkbox" name="add_info_approved" value="yes" /></td> <td><input type="submit" name="add_info_button" value="Add New Info" /></td> </tr> </table> </form> The add_info_handler.php script obtains the value as follows: $_add_info_value = $_REQUEST['add_info_approved']; if ($_add_info_value == "yes"){ $_info_check = "Y"; }else{ $_info_check = "N"; } I want to then place this value into a table in MySQL: $q = "INSERT INTO info_table (add_info_check) VALUES ($_info_check)"; $r = @mysqli_query ($dbc, $q); However, this does not seem to boe working, even though the query is correct and the fields match those in the table. I have printed out $_info_check and that contains the correct value. Is there something else I should be doing with the check box? Or is is because I am using REQUEST? I have tried to use post but this does not work either. There are other values I have ommited which are placed into the table when the value above is not placed into the query so I know I have the connection etc. to the DB. Thanks. MB Quote Link to comment Share on other sites More sharing options...
Adam Posted July 3, 2009 Share Posted July 3, 2009 Field values need quotes around them if they're strings: $q = "INSERT INTO info_table (add_info_check) VALUES ('{$_info_check}')"; Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 3, 2009 Share Posted July 3, 2009 I would also like to add some suggestions. You are storing a boolean value - one which is either true or false. It is standard procedure to store the value as either 0 (false) or 1 (true). All programming languages (at least those that I am aware of) will inherently interprest a 1 and 0 as true and false. So, if you do a comparison, you can do this if($info_check) instead of this if($info_check=='yes') This makes your code much for efficient. Also, the value of a checkbox in a form field is hidden from the user. So there is no reason to make the values yes/no and then change it on the processing page. Simply make the values of the checkboxes the values you will use in the processing page. But, don't use that as an excuse not to ensure that malicious code is not being submitted. Assuming the value of the checkbox is '1', my processing code would do something like this $value = ($_POST['field_name']=='1'); 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.