【问题标题】:how to store two checkbox values in mysql db using php如何使用php在mysql db中存储两个复选框值
【发布时间】:2016-06-14 16:08:37
【问题描述】:

我的表单中有多个复选框值,如图所示

我知道我可以将单个复选框的值存储在 db 中,但这里的情况略有不同,所以我能否成功地将它们存储在同一个 db 的不同行中。

我的表结构是

   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 

我必须在 content_table 中插入显示形式的数据,该表格按 category_id、course_id、subject_id、content_type_id 存储数据

我已经尝试过以下代码,它只适用于单个复选框,即类别,所以请有人建议我如何为不同类型的复选框做些什么。

我的代码是

<?php
include 'includes/dbconfig.php';
if($_SERVER['REQUEST_METHOD']=='GET')
{
echo $title = $_GET['title'];
echo $description = $_GET['description'];
echo $content_url = $_GET['content_url'];
echo $thumb_icon_url = $_GET['thumb_icon_url'];
   $category = $_GET['category'];
   $course = $_GET['course'];
echo $select_subject = $_GET['select_subject'];
echo $select_content_type  = $_GET['select_content_type'];
foreach (array_combine($category, $course) as $cat => $cose) {
    $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);
    echo $category_id = $row['category_id'];
    $sql1 = mysqli_query($conn,"SELECT course_id from course_ref_table where course = '$cose'") or die(mysqli_error());
    $row1 = mysqli_fetch_array($sql1,MYSQLI_ASSOC);      
    echo $course_id = $row1['course_id'];
    $sql2 = mysqli_query($conn,"SELECT subject_id from subject_ref_table where subject = '$select_subject'") or die(mysqli_error());
    $row2 = mysqli_fetch_array($sql2,MYSQLI_ASSOC);      
    echo $subject_id = $row2['subject_id'];
    $sql3 = mysqli_query($conn,"SELECT content_type_id from content_type_table where content_type = '$select_content_type'") or die(mysqli_error());
    $row3 = mysqli_fetch_array($sql3,MYSQLI_ASSOC);      
    echo $content_type_id = $row3['content_type_id'];
    mysqli_query($conn,"INSERT INTO content_ref_table (title,description,content_url,thumb_icon_url,category_id,course_id,subject_id,content_type_id,login_id)VALUES ('$title','$description','$content_url','$thumb_icon_url','$category_id','$course_id','$subject_id','content_type_id')");

 } 


 }
 ?>

【问题讨论】:

  • 循环中的查询使您的项目变慢使用联接而不是使用多个查询
  • 我仍然没有得到可行的答案@Ms.Nehal
  • 如果可以针对单个课程/主题存储多个类别,那么您的表格设计有缺陷
  • ...这里有很多查询。为什么?

标签: php mysql checkbox


【解决方案1】:

试试这样的(数组变量)

<input type="checkbox" name="category[]" value="entrance">
<input type="checkbox" name="category[]" value="School">
<input type="checkbox" name="category[]" value="ug">
<input type="checkbox" name="category[]" value="pg">

这样使用后,您可以将检查项目列表作为数组获取。之后按要求将其保存在您的表格中。

【讨论】:

  • 我有两个数组,一个用于类别,另一个用于课程,单个复选框数组一切正常
猜你喜欢
  • 2013-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-18
  • 2012-12-11
  • 1970-01-01
  • 2018-12-18
  • 1970-01-01
相关资源
最近更新 更多