问题
I have multiple checkbox values as shown in figure:
I know that can store values for single checkbox in DB but situation here is slightly different, so how could I successfully store them in different rows for the same DB.
My table structure is:
1.content_table
title,description,category_id(fk),course_id(fk),subject_id(fk),content_type_id(fk)
2.category_table
entrance,school,ug,pg
3.subject_table
english,hindi,maths......
4.content_type_table
notes,summary,videos,question_bank
I have to insert data from shown form in content_table which stores data by category_id,course_id,subject_id,content_type_id.
I have tried the following code which is only working for single checkbox i.e category , so please someone suggest to me how to do it for a different type of checkbox.
My code is:
$title = $_POST['title'];
$description = $_POST['description'];
$url = $_POST['content_url'];
$category = $_POST['category'];
foreach ($category as $cat) {
$sql = mysqli_query($conn,"SELECT category_id from category_ref_table where category = '$cat'") or die(mysqli_error());
$row = mysqli_fetch_array($sql,MYSQLI_ASSOC);
$category_id = $row['category_id'];
mysqli_query($conn,"INSERT INTO content_table (title,description,url,category)VALUES ('$title','$description','$url','$category_id')");
}
来源:https://stackoverflow.com/questions/37782600/how-to-store-multiple-checkbox-values-in-different-rows-in-mysql-db