繁体   English   中英

将多个记录从复选框更新到数据库

[英]Update multiple records from checkbox to database

multiinput.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.phpmultiedit.php以处理multiedit.php

不知道出了什么问题,但更新功能不起作用。 我很确定需要从multiedit.phphandlemultiedit.php传递一个ID。 PS我是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或checked事件来处理隐藏的文本框值。

[编辑]检查此代码

附加说明:出于调试目的,将以下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文件添加到标题中。

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM