I am trying to get the value from the checkbox selected in a form inserted into a database using php and sql. I have included the html and some php I am trying to get to work. The first bit of php works great. The second bit of php is one of many attempts to get the value of the checkbox from the form inserted into a database. Thank you for your help!
<?php
$sql = "INSERT into students set student_number = '{$_POST['student_number']}',
first_name = '{$_POST['first_name']}', last_name = '{$_POST['last_name']}', degree =
'{$_POST['degree']}'";
mysql_query($sql);
?>
<?php
$classes->bind_result($class);
$classes->execute();
$class = array();
while ($classes->fetch()) {
$class[] = $class;
}
?>
<div class="div1">COP1000:</div><input class="checkbox1" type="checkbox"
value="COP1000" name="class[]" id="class[]" /><br />
<div class="div1">COP2800:</div><input class="checkbox1" type="checkbox"
value="COP2800" name="class[]" id="class[]" /><br />
<div class="div1">CIS2910C:</div><input class="checkbox1" type="checkbox"
value="CIS2910C" name="class[]" id="class[]" /><br />
<div class="div1"> COP2830:</div><input class="checkbox1" type="checkbox"
value="COP2830" name="class[]" id="class[]" /><br /><br />
$_POST['class']is an array.You can insert these values into one field your database by (naturally) turn it into a string. The easiest way would be:
which puts a comma (,) between your values. When you want to read all values you can use
explode()like this:If you wish to loop through every value and perform an update/action on every value, you should loop through the array:
Important (irrelevant) note: You should sanitize your input before inserting it into your database. If not, users can extend your SQL query and basically can do anything with your database. For more information, click bazmegakapa’s link.