php - Saving multiple check boxes into one database field -
this question has answer here:
i have number of check boxes relate 1 field in database. checked values recognised , put string value not saving database. table name , field name both correct. have ideas doing wrong? many in advance.
*i understand code provided not secure please note testing implode function before apply actual code.
my html code is:
<tr> <td> <input type="checkbox" name="test[]" value="'apple'">apple<br> <input type="checkbox" name="test[]" value="'banana'">banana<br> <input type="checkbox" name="test[]" value="'pear'">pear<br> <input type="checkbox" name="test[]" value="'grapes'">grapes<br> <input type="checkbox" name="test[]" value="'pineapple'">pineapple<br> <input type="checkbox" name="test[]" value="'strawberries'">strawberries </td> </tr>
my php code is:
print_r($_post); session_start(); $p_test = $_post["test"]; $in = implode(',', $p_test); include ("dbconnect.php"); $dbquery = "insert test(testvalues) values($in)"; echo $dbquery; $result = mysql_query($dbquery);
the $_post , $dbquery being printed screen as:
array ( [test] => array ( [0] => 'apple' [1] => 'banana' [2] => 'pear' ) [sbp] => n/a [submit] => submit )
insert test(testvalues) values('apple','banana','pear')
it getting checked values , putting them string values not going database field.
insert test(testvalues) values('apple','banana','pear')
your sql trying insert 3 values 1 column.
not sure why html has quotes included need escaped regardless. switch pdo (mysql_*
functions deprecated) , use parameterized queries. way automatically escaped.
Comments
Post a Comment