多输入.php
<form name="form" id="form" action="multiedit.php" method="post">
<div id="show">
</div>
<p><table>
<tr>
<th>Tick</th>
<th>Name</th>
<th>Rank</th>
<th>Start Date</th>
<th>End Date</th>
<th>Watchkeeping</th>
<th>Active</th>
</tr> <!-- database -->
<tr>
<?php
if (!mysqli_connect_errno($con)) {
$queryStr = "SELECT * " .
"FROM crewlist";
}
$result = mysqli_query($con, $queryStr);
while ($row = mysqli_fetch_array($result)) {
if (date("Y-m-d") > $row['start_date'] && date("Y-m-d") < $row['end_date']) {
echo "<tr><th>" . "<input type = 'checkbox' name = 'checkbox2[]' value='" . $row['crew_id']. "'>" . "</th>";
echo "<th>" . "<a href=\"viewcrew.php?id=" . $row['crew_id'] . "\">" . $row["crew_name"] . "</a>";
echo "<th>" . $row["crew_rank"] . "</th>";
echo "<th>" . $row["start_date"] . "</th>";
echo "<th>" . $row["end_date"] . "</th>";
echo "<th>" . $row["watchkeeping"] . "</th>";
echo "<th>" . $row["active"] . "</th>";
}
}
?>
</tr>
<input type="submit" name="submit"value="Submit" ></td>
</tr>
</table>
</form>
multiedit.php
<?php
require ("checkloginstatus.php");
include 'header.php'; ?>
<div id="container4"><?php
require ("dbfunction.php");
$con = getDbConnect();
$checkbox2 = $_POST['checkbox2'];
if (!mysqli_connect_errno($con)) {
$str = implode($checkbox2);
$queryStr = "SELECT * " .
"FROM crewlist WHERE ($str)";
}
$result = mysqli_query($con, $queryStr);
if ($_POST['submit']) {
$checkbox2 = $_POST['checkbox2'];
foreach ($checkbox2 as $crewname) {
?> <form action="handlemultiedit.php" method="post">
<input type="hidden" name="crew_id" value="<?php echo $_GET['id']; ?>" />
<?php echo "<tr><th>" . $crewname . ":</th><br>";
echo " <tr>
<td>Shift 1:</td>
<td><input type=\"time\" name=\"start_hour\" value=\"start_hour\" id=\"start_hour\" step=\"1800\" required> to <input type=\"time\" name=\"end_hour\" value=\"end_hour\" id=\"end_hour\" step=\"1800\" required>
</td>
</tr>
<tr>
<td>Shift 2:</td>
<td><input type=\"time\" name=\"start_hour2\" value=\"start_hour2\" id=\"start_hour2\" step=\"1800\" required> to <input type=\"time\" name=\"end_hour2\" value=\"end_hour2\" id=\"end_hour2\" step=\"1800\" required>
</td>
</tr><br><br>";
// print_r($_POST);
?>
<?php
}?>
<td><input type="submit" name="submit" value="Submit" ></td></form>
<?php
}
?>
3) handlemultiedit.php
<?php
print_r($_POST);
require 'dbfunction.php';
$con = getDbConnect();
$crew_id = $_POST["crew_id"];
$start_hour = $_POST["start_hour"];
$end_hour = $_POST["end_hour"];
$start_hour2 = $_POST["start_hour2"];
$end_hour2 = $_POST["end_hour2"];
//if (!mysqli_connect_errno($con)) {
//
// $queryStr = "SELECT * " .
// "FROM crewlist";
//
//$result = mysqli_query($con, $queryStr);
//}
if (isset ($_POST["submit"]) && $_POST['submit'] !=="") {
$usercount = count ($_POST['crew_id']);
for($i=0;$i<$usercount;$i++) {
$sqlQueryStr = "UPDATE crewlist SET start_hour = '" . $_POST["start_hour"] . "',end_hour = '$end_hour', start_hour2 = '$start_hour2',end_hour2 = '$end_hour2' WHERE crew_id = " . $crew_id . "";
mysqli_query($con, $sqlQueryStr);
}
}
//header('Location: crewlisting.php');
mysqli_close($con);
?>
我的网页流程是multiinput.php
到multiedit.php
来处理multiedit.php
不确定哪里出了问题,但更新功能不起作用。我很确定需要有一个 ID 从 multiedit.php
传递到 handlemultiedit.php
。 P.S 我是 php 的新手,所以请指教我的错误和改进。我的意图是让管理员使用复选框选择多个用户,然后单击提交按钮,这会为管理员编辑他们的工作时间表。
最佳答案
复选框在表单提交中有点棘手。让我解释一下使用复选框时会发生什么。如果未选中该复选框,尽管提交了表单,该复选框仍被视为未发布。但在文本框的情况下,如果文本框为空,则空文本框将在提交时发布。
所以在这种情况下,数组 checkbox2[]
的未检查数组元素将不会被提交,仅使用检查值移动数组值。这使得结果错误。
例子: 这是您的表单值
checkbox2[0] is checked // this will be submitted
checkbox2[1] is unchecked //this wont be submitted
checkbox2[2] is checked // this will be submitted
提交表单后,数据将如下所示。错误&不符合预期
checkbox2[0]=checked
checkbox2[1]=checked
因此,使用多个复选框的方式,您可以为每个复选框使用一个隐藏的文本框,使用 javascript onchange 来根据相应复选框的 onchange 或选中事件操作隐藏的文本框值。
[编辑] 检查这段代码
附加说明:出于调试目的,将以下 php 代码添加到 multiedit.php 以查看提交的值
if($_POST){
echo '<pre>';
print_r($_POST);
echo '</pre>';
像这样编辑代码中的以下部分
$i=0;
while ($row = mysqli_fetch_array($result)) {
if (date("Y-m-d") > $row['start_date'] && date("Y-m-d") < $row['end_date']) {
//checkbox modified
echo "<tr><th>" . "<input type = 'checkbox' id='dummy[".$i."]' name = 'checkbox2[".$i."]' value='" . $row['crew_id']. "'>" . "</th>";
//added hidden textbox to carry value
echo "<input type ='hidden' id='h".$i."' name = 'htext[".$i."]'/>";
echo "<th>" . "<a href=\"viewcrew.php?id=" . $row['crew_id'] . "\">" . $row["crew_name"] . "</a>";
echo "<th>" . $row["crew_rank"] . "</th>";
echo "<th>" . $row["start_date"] . "</th>";
echo "<th>" . $row["end_date"] . "</th>";
echo "<th>" . $row["watchkeeping"] . "</th>";
echo "<th>" . $row["active"] . "</th>";
$i++;
}
}
在页面底部添加以下jquery。请记住将 jquery.js 文件添加到 header 。
<script>
$("input[type='checkbox']").click(function(){
var i=$(this).attr("id").match(/\d+/);
var v=$(this).attr("value");
if ($(this).is(":checked"))
{$("#h"+i).val(v);}
else
{$("#h"+i).val(null);}
});
</script>
补充:不要将以下部分视为答案的一部分
希望这对您有所帮助。我已经测试过,它工作正常。
作为额外的引用,这里是我用来测试的test.php,以便更好地理解
<html>
<head>
<script src="jquery.min.js"></script>
</head>
<body>
<form method="post" action="">
<?php
if($_POST){
echo '<pre>';
print_r($_POST);
echo '</pre>';
$checked=0;
}
for($i=0;$i<5;$i++){
echo "<input type = 'checkbox' id='dummy[".$i."]' name = 'checkbox2[".$i."]' value='my value" .$i. "'>";
echo "<input type ='text' id='h".$i."' name = 'htext[".$i."]'/>";
}
?>
<input type="submit" name="sub" />
<script>
$("input[type='checkbox']").click(function(){
var i=$(this).attr("id").match(/\d+/);
alert(i);
var v=$(this).attr("value");
alert(v);
if ($(this).is(":checked"))
{$("#h"+i).val(v);}
else
{$("#h"+i).val(null);}
});
</script>
</body>
</html>
关于php - 将多条记录从复选框更新到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32900099/