当数据来自表单上的多选(复选框)字段时,我想为每门学生课程组合分配多门单独的课程给学生。也就是说,将数据作为包含多个课程的单个结果返回,我想将其拆分。
简短版本:老师从一个下拉菜单中选择了多个课程,但我想将数据保存为多行。
这是我的代码。
<?php
$con = mysqli_connect('localhost','root');
mysqli_select_db($con,'sss_qr');
$q="select * from course_tb ";
$result=mysqli_query($con,$q);
?>
<!DOCTYPE html>
<html>
<head>
<title>Webslesson Tutorial | Bootstrap Multi Select Dropdown with Checkboxes using Jquery in PHP</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.2/bootstrap3-typeahead.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/css/bootstrap-multiselect.css" />
</head>
<body>
<br /><br />
<div class="container" style="width:600px;">
<h2 align="center">Bootstrap Multi Select Dropdown with Checkboxes using Jquery in PHP</h2>
<br /><br />
<form method="post" id="framework_form">
<div class="form-group">
<label>Select which Framework you have knowledge</label>
<select id="framework" name="framework[]" multiple class="form-control" >
<?php
if($result)
{
while($row=mysqli_fetch_array($result))
{
$course=$row["course_name"];
$code=$row["course_code"];
echo "<option value='$code'>$course</option>";
}
}
?>
</select>
</div>
<div class="form-group">
<input type="submit" class="btn btn-info" name="submit" value="Submit" />
</div>
</form>
<br />
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('#framework').multiselect({
nonSelectedText: 'Select Framework',
enableFiltering: true,
enableCaseInsensitiveFiltering: true,
buttonWidth:'400px'
});
$('#framework_form').on('submit', function(event){
event.preventDefault();
var form_data = $(this).serialize();
$.ajax({
url:"insert.php",
method:"POST",
data:form_data,
success:function(data)
{
$('#framework option:selected').each(function(){
$(this).prop('selected', false);
});
$('#framework').multiselect('refresh');
alert(data);
}
});
});
});
</script>
和insert.php文件
<?php
$connect = mysqli_connect("localhost", "root", "", "sss_qr");
if(isset($_POST["framework"]))
{
$framework = '';
foreach($_POST["framework"] as $row)
{
$framework .= $row . ', ';
}
$framework = substr($framework, 0, -2);
$query = "INSERT INTO allocoursestudent(course_code) VALUES('".$framework."')";
if(mysqli_query($connect, $query))
{
echo 'Data Inserted';
}
}
?>
答案 0 :(得分:0)
首先,您需要创建一个将学生和课程配对的新表。 其次,您需要在表单(下拉菜单)中显示学生。
之后,您可以执行以下操作:
<?php
$connect = mysqli_connect("localhost", "root", "", "sss_qr");
if(isset($_POST["course_code"])){
$course_code = mysqli_real_escape_string($con, $_POST["course_code"]);
$student_id = mysqli_real_escape_string($con, $_POST["student_id"]);
$query = "INSERT INTO new_table_name(course_code, student_id) VALUES";
for($i=0; $i<count($course_code); $i++){
$query .= " ('".$course_code[$i] ."', '".$student_id[$i]."'),";
}
$query = substr($query, 0, -1);
if(mysqli_query($connect, $query)){
echo 'Data Inserted #'.$i;
}
}
?>
答案 1 :(得分:0)
所以我通过使用此代码解决了这个问题。
<?php
$con = mysqli_connect("localhost", "root", "", "sss_qr");
$course=$_POST['course'];
if($course)
{
foreach($course as $c)
{
$q="INSERT INTO allocoursestudent(course_code) VALUES('".$c."')";
mysqli_query($con,$q);
}
}
?>