How to store multiple checkbox values in different rows in MySQL db?

╄→гoц情女王★ 提交于 2020-01-04 05:28:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!