gpt4 book ai didi

php - 从输入 [] 获取数据时从 PDO 中的数组重复插入时循环困惑

转载 作者:行者123 更新时间:2023-11-29 05:53:41 26 4
gpt4 key购买 nike

  • 有一个特殊的出勤和缺勤项目作为大学的职责
  • 我创建了一个表单,通过使用 [] 到 git 数组来 git 他的输入表单数据库
  • 一切正常但是:
  • ** 当我从我的表单中插入数据时,我找不到正确的方法让它正常工作 **
  • 我曾经循环 ** for ** 以转到下一个索引和 ** foreach ** 以获取单选按钮的索引

这是我的表格,我从数据库中获取了它:

table get input data form database

<br>
<div class="row justify-content-md-center">
<h2>شاشة ادخال الحضور والغياب للطالب </h2>
<br><br>
</div>
<br><br>

<form action="" dir="rtl" method="POST">
<fieldset>
<legend>اختيار بيانات التحظير :</legend>
<div class="row">
<div class="col">
<select class="form-control" name="phase_student">
<option value="">المرحلة</option>
<?php
$QueryDB = "SELECT * FROM `phase_student`";
$DoQuery = mysqli_query($Con, $QueryDB);
$ChekRows = mysqli_num_rows($DoQuery);


if ($ChekRows > 0) {
while ($Rows = mysqli_fetch_assoc($DoQuery)) {
echo "<option value='" . $Rows['num_phase'] . "'> " . $Rows['phase'] . " </option>";
}
} else {
echo "<option value=''>لا توجد بيانات مرحلة </option>";
} ?>
</select>
</div>
<div class="col">

<select class="form-control" name="classe">
<option value="">الصف</option>
<?php

$QueryDB2 = "SELECT * FROM `classe`";
$DoQuery2 = mysqli_query($Con, $QueryDB2);
$ChekRows2 = mysqli_num_rows($DoQuery2);

if ($ChekRows2 > 0) {
while ($Rows2 = mysqli_fetch_assoc($DoQuery2)) {
echo "<option value='" . $Rows2['num_class'] . "'> " . $Rows2['class'] . " </option>";
}
} else {
echo "<option value=''>لا توجد بيان الصفوف </option>";
} ?>

</select>
</div>
<div class="col">

<select class=" form-control" name="prong">
<option value="">الشعبة</option>
<?php

$QueryDB3 = "SELECT * FROM `prong`";
$DoQuery3 = mysqli_query($Con, $QueryDB3);
$ChekRows3 = mysqli_num_rows($DoQuery3);

if ($ChekRows3 > 0) {
while ($Rows3 = mysqli_fetch_assoc($DoQuery3)) {
echo "<option value='" . $Rows3['num_prong'] . "'> " . $Rows3['prong'] . " </option>";
}
} else {
echo "<option value=''>لا توجد بيان شعب </option>";
} ?>
</select>
</div>

</div>

<br>
<div class="row">
<div class="col">
<select class="form-control" name="item">
<option value="">اسم المادة</option>
<?php


$QueryDB6 = "SELECT * FROM `item`";
$DoQuery6 = mysqli_query($Con, $QueryDB6);
$ChekRows6 = mysqli_num_rows($DoQuery6);

if ($ChekRows6 > 0) {
while ($Rows6 = mysqli_fetch_assoc($DoQuery6)) {
echo "<option value='" . $Rows6['num_item'] . "'> " . $Rows6['item'] . " </option>";
}
} else {
echo "<option value=''>لا توجد بيان المواد </option>";
} ?>
</select>
</div>
<div class="col">

<select class=" form-control" name="monthe">
<option value="">الشهر</option>
<?php

$QueryDB5 = "SELECT * FROM `monthe`";
$DoQuery5 = mysqli_query($Con, $QueryDB5);
$ChekRows5 = mysqli_num_rows($DoQuery5);

if ($ChekRows5 > 0) {
while ($Rows5 = mysqli_fetch_assoc($DoQuery5)) {
echo "<option value='" . $Rows5['num_month'] . "'> " . $Rows5['monthe'] . " </option>";
}
} else {
echo "<option value=''>لا توجد بيان الاشهر </option>";
} ?>
</select>
</div>

<div class="col">

<input type="date" class="form-control" name="thisDay" value="">
</div>

</div>
</fieldset>


<br>
<br>


<div class="container">

<table class="table table-bordered">
<thead>
<tr>
<th>متسلسل</th>
<th>اسم الطالب</th>
<th>حاضر</th>
<th>غائب</th>
<th>مستئذن</th>
<th> ملاحظات</th>

</tr>
</thead>
<tbody>
<tr>
<?php

$queryStu = "SELECT * FROM `student_academia` ";
$DoQuerystu = mysqli_query($Con, $queryStu);
$ChekRowsstu = mysqli_num_rows($DoQuerystu);

if ($ChekRowsstu > 0) {
while ($Rowsstu = mysqli_fetch_assoc($DoQuerystu)) {
echo " <td> <span> " . $Rowsstu['num__student'] . "</span> <input type='hidden' name ='IdStu[]' value='" . $Rowsstu['num__student'] . "' > </td>";
echo " <td> <span> " . $Rowsstu['name_student'] . "</span> <input type='hidden' name ='NaStu[]' value='" . $Rowsstu['name_student'] . "' > </td>";
echo " <td>
<div class=\"form-check\">
<label class=\"form-check-label\">
<input class=\"form-check-input\" type=\"radio\" name=\"attend[" . $Rowsstu['num__student'] . "]\" value=\"1\" id=" . $Rowsstu['num__student'] . ">
</label>
</div>
</td>
<td>
<div class=\"form-check\">
<label class=\"form-check-label\">
<input class=\"form-check-input\" type=\"radio\" name=\"attend[" . $Rowsstu['num__student'] . "]\" value=\"0\" id=" . $Rowsstu['num__student'] . ">
</label>
</div>
</td>
<td>
<div class=\"form-check\">
<label class=\"form-check-label\">
<input class=\"form-check-input\" type=\"radio\" name=\"excusable[" . $Rowsstu['num__student'] . "]\" value=\"1\" id=" . $Rowsstu['num__student'] . ">
<input class=\"form-check-input sr-only\" type=\"radio\" name=\"excusable[" . $Rowsstu['num__student'] . "]\" value=\"0\" id=" . $Rowsstu['num__student'] . " checked >
</label>
</div>
</td>
<td><input type='text' class='form-control' name='attendNote[]' placeholder='ملاحظات ' /></td>
</tr>
";
}
} else {
echo "لا توجد بيان طلاب";
}

?>


</tbody>

</table>
</div>

<br>
<br>

<div class=" row justify-content-center">
<div class="col-3">
<input class="btn btn-outline-primary btn-block" type="submit" name="savedata"
value="حفظ"/>
</div>
<div class="col-3">
<input class="btn btn-block btn-outline-warning " type="reset" value="مسح"/>
</div>
<div class="col-3">
<button type="button" class="btn btn-block btn-outline-danger" name="close">خروج
</button>
</div>
</div>
</form>

这是我发布表格时的提示:

<?php
include "header.php";
//connect to data base oraginal con
include "ConDB.php";
// connect to database but Php Data Opject conect
$db = new PDO("mysql:host=localhost;dbname=school_db", "root", "");
// utf8 supurt arabic
$db->exec("SET CHARACTER SET utf8");
// show error on connect
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// when some body click save button
if (isset($_POST['savedata'])) {

$phase_student = $_POST['phase_student'];
$classe = $_POST['classe'];
$prong = $_POST['prong'];
$item = $_POST['item'];
$monthe = $_POST['monthe'];
$thisDay = $_POST['thisDay'];
//array
$IdStu = $_POST['IdStu'];
$NaStu = $_POST['NaStu'];
$attends = $_POST['attend'];
$excusables = $_POST['excusable'];
$attendNote = $_POST['attendNote'];


echo "<br>";
var_dump($NaStu);

echo "<br>";
var_dump($IdStu);
echo "<br>";
var_dump($attends);
echo "<br>";
var_dump($attendNote);
echo "<br>";
var_dump($excusables);

echo "<br>";
/*
$queryInsertAttend = "INSERT INTO `outcom_month`(`num__student`, `name_student`, `num_month`, `num_class`,`class`, `prong`, `num_item`, `item`, `duties(20)`, `oral(20)`, `assiduity(20)`, `edit(40)`, `total`, `tribnte`, `note_avant`, `note_guardian`) VALUES (
$num__student ,$num__student, $num_month ,$num_class,$num_class,$prong,$num_item,$num_item,
$duties,$oral,$assiduity,$edit,$edit,$tribnte,$note_avant,$note_avant)";

**/
//
// $id = new ArrayIterator($IdStu);
// $at = new ArrayIterator($attend);
// $atnot = new ArrayIterator($attendNote);
//
// $dataa = new MultipleIterator(MultipleIterator::MIT_NEED_ALL);
//
// $dataa->attachIterator($id);
// $dataa->attachIterator($at);
// $dataa->attachIterator($atnot);
////
if (!empty($_POST)) {

//$insert_stu_Attend = $db->prepare("insert into test values (?,?,?,?,?,?,?,?)");
$insert_stu_Attend = $db->prepare("insert into test2 values (?,?,?,?,?,?,?,?)");
$insert_stu_Attend->bindParam(1, $first);
$insert_stu_Attend->bindParam(2, $second);
$insert_stu_Attend->bindParam(3, $third);
$insert_stu_Attend->bindParam(4, $forth);
$insert_stu_Attend->bindParam(5, $fifth);
$insert_stu_Attend->bindParam(6, $sixth);
$insert_stu_Attend->bindParam(7, $seventh);
$insert_stu_Attend->bindParam(8, $eighth);

for ($i = 0; $i < 3; $i++) {
foreach ($IdStu as $s) {
$dd = 1;
$first = $IdStu[$i];
$second = $monthe;
$third = $item;
$forth = $classe;
$fifth = $thisDay;
$sixth = $attends[$s];
$seventh = $excusables[$s];
$eighth = $attendNote[$i];
$insert_stu_Attend->execute();
}
}
}


} else {
echo '<br>';
echo " لم يتم الادخال";
}

?>

每次我得到表格中唯一的第一行。

标签定义:

CREATE TABLE test (
num__student int(11) NOT NULL UNIQUE,
year_study date NOT NULL,
num_item int(11) NOT NULL,
calss varchar(15) NOT NULL,
date_day date NOT NULL,
attendee tinyint(1) NOT NULL,
excusable tinyint(1) NOT NULL,
nate mediumtext NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

最佳答案

显然您使用的 bindParam 不是最正确的方式。

根据这个:https://www.w3schools.com/php/php_mysql_prepared_statements.asp ,您应该在循环之前绑定(bind)它们,并使用循环仅适配您绑定(bind)的变量。

我将使用您的代码作为示例,好吗?

if (!empty($_POST)) {
$insert_stu_Attend = $db->prepare("insert into test values (?,?,?,?,?,?,?,?)");
$insert_stu_Attend->bindParam(1, $first);
$insert_stu_Attend->bindParam(2, $second);
$insert_stu_Attend->bindParam(3, $third);
$insert_stu_Attend->bindParam(4, $forth);
$insert_stu_Attend->bindParam(5, $fifth);
$insert_stu_Attend->bindParam(6, $sixth);
$insert_stu_Attend->bindParam(7, $seventh);
$insert_stu_Attend->bindParam(8, $eighth);

foreach ($IdStu as $key => $s) {
$dd = 1;
$first = $IdStu[$key];
$second = $monthe;
$third = $item;
$forth = $classe;
$fifth = $thisDay;
$sixth = $attends[$s];
$seventh = $excusables[$s];
$eighth = $attendNote[$key];
$insert_stu_Attend->execute();
}
}

根据文档,它应该可以工作。让我知道进展如何。

编辑我删除了 for 并仅保留了 foreach,因为考虑到为此结果发送的用户数量,我假设您正在使用 FOR 循环遍历来自不同数组的结果。

关于php - 从输入 [] 获取数据时从 PDO 中的数组重复插入时循环困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51774899/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com