【发布时间】: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
-
如果可以针对单个课程/主题存储多个类别,那么您的表格设计有缺陷
-
...这里有很多查询。为什么?