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

时间:2015-10-02 03:24:02

标签: php mysql

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.php传递给handlemultiedit.php。 P.S我是php的新手,所以请告诉我错误和改进。我的目的是让管理员使用复选框选择多个用户,然后单击提交按钮,这将为管理员编制工作时间表。

1 个答案:

答案 0 :(得分:0)

Checkbox在表单提交方面有点棘手。让我解释一下使用复选框时会发生什么。如果取消选中该复选框,则虽然表单已提交,但复选框仍被视为未发布。但是如果文本框为空,则文本框为空时,将在提交时发布文本框。 所以在这种情况下,数组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

发布表单时,数据将如下所示。错误的&amp;不像预期的那样

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>
相关问题