[英]How to insert array of post data to DB
I have a list of items was gotten from my database, then I'm using array to post the data through a from in order to insert them to my database since they are more than one.我有一个从我的数据库中获取的项目列表,然后我使用数组通过 from 发布数据,以便将它们插入到我的数据库中,因为它们不止一个。
<form method="post" actioin="courses.php">
<table>
<thead>
<tr>
<th>S/N</th>
<th>Action</th>
<th>Course Title</th>
<th>Course Code</th>
<th>Course Unit</th>
</tr>
</thead>
<tbody>
<?php
$stmt = "SELECT * FROM courses WHERE level ='$regl'AND semester='$regsem'";
$runStmt = $db->query($stmt);
$counter = 0;
while($show = $runStmt->fetch_array(MYSQLI_ASSOC))
{
$id=$show['course_id'];
$ctitle=$show['course_title'];
$ccode=$show['course_code'];
$cunit=$show['course_unit'];?>
<tr>
<td><?php echo ++$counter?></td>
<td>
<input type="checkbox" value="<?php echo $id?>" name="isChecked['course_id']">
</td>
<td><input type="hidden" value="<?php echo $ctitle?>" name="isChecked['course_title']"><?php echo $ctitle?>
</td>
<td><input type="hidden" value="<?php echo $ctitle?>" name="isChecked['course_code']"> <?php echo $ccode?>
</td>
<td><input type="hidden" value="<?php echo $ctitle?>" name="isChecked['course_unit']">
<?php echo $cunit?>
</td>
</tr>
<?php }?>
</tbody>
</table>
<input type ="submit" value="enroll" name="enroll">
</form>
If a checkbox is checked, How can I get all the values of the array (title, code, unit) and insert them on its corresponding column in my enroll table also note that the courses are more than 1?如果选中了一个复选框,我如何获取数组的所有值(标题、代码、单位)并将它们插入到我的注册表的相应列中,还请注意课程超过 1 个?
so i have something on ground but it's way out of line.. so your corrections/suggestions will do:所以我有一些东西,但它有点不合时宜..所以你的更正/建议会做:
<?php
if(isset($_POST['enroll'])){
$i=0;
if(!empty ($_POST['isChecked'])){
$stmt = $db->prepare("INSERT INTO courses (course_id, course_title, course_code, course_unit,
dept,level,session)
VALUES (?, ?, ?,?,?,?,?)");
$stmt->bind_param("ississs", $cid, $ctitle, $ccode,$cunit,$dpt,$lvl,$sessn);
foreach($_POST['isChecked'] as $course){
$course = $_POST['isChecked'][$i];
$ctitle = $_POST['isChecked'][$i];
$ccode = $_POST['isChecked'][$i];
$cunit = $_POST['isChecked'][$i];
$dpt = $stud_dept;
$lvl = $regl;
$sessn = $regs;
$stmt->execute();
$i++;
}
}
}
?>
You can use javascript for it, just change the first row like this:您可以使用 javascript ,只需像这样更改第一行:
<form method="post" actioin="courses.php" onChange="this.form.submit()" >
of to compline with the critics put it at the checkbox为了符合批评者的要求,请将其放在复选框中
<form method="post" actioin="courses.php">
<table>
<thead>
<tr>
<th>S/N</th>
<th>Action</th>
<th>Course Title</th>
<th>Course Code</th>
<th>Course Unit</th>
</tr>
</thead>
<tbody>
<?php
$stmt = "SELECT * FROM courses WHERE level ='$regl'AND semester='$regsem'";
$runStmt = $db->query($stmt);
$counter = 0;
while($show = $runStmt->fetch_array(MYSQLI_ASSOC))
{
$id=$show['course_id'];
$ctitle=$show['course_title'];
$ccode=$show['course_code'];
$cunit=$show['course_unit'];?>
<tr>
<td><?php echo ++$counter?></td>
<td>
<input type="checkbox" value="<?php echo $id?>" name="isChecked['course_id']" onChange="this.form.submit()">
</td>
<td><input type="hidden" value="<?php echo $ctitle?>" name="isChecked['course_title']"><?php echo $ctitle?>
</td>
<td><input type="hidden" value="<?php echo $ctitle?>" name="isChecked['course_code']"> <?php echo $ccode?>
</td>
<td><input type="hidden" value="<?php echo $ctitle?>" name="isChecked['course_unit']">
<?php echo $cunit?>
</td>
</tr>
<?php }?>
</tbody>
</table>
<input type ="submit" value="enroll" name="enroll">
</form>
<?php
if(isset($_POST['enroll'])){
$i=0;
if(!empty ($_POST['isChecked'])){
$stmt = $db->prepare("INSERT INTO courses (course_id, course_title, course_code, course_unit,
dept,level,session)
VALUES (?, ?, ?,?,?,?,?)");
$stmt->bind_param("ississs", $cid, $ctitle, $ccode,$cunit,$dpt,$lvl,$sessn);
// here is your mistake you assign $_POST['isChecked'] as $course,
// then you have to use $course
//
// the next failure is you fetched the data with MYSQLI_ASSOC
// then you must call them by name like course['course_id'];
//
// what you receive is a variable called like :
// $_POST['isChecked']['course_unit']['$i']
// so you will have in your foreach $course['course_unit']['$i']
//
foreach($_POST['isChecked'] as $course){
$cid = $course['course_id']['$i'];
$ctitle = $course['course_title']['$i'];
$ccode = $course['course_code']['$i'];
$cunit = $course['course_unit']['$i'];
$dpt = $stud_dept;
$lvl = $regl;
$sessn = $regs;
$stmt->execute();
$i++;
}
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.