简体   繁体   中英

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); 
?>

The flow of my webpage is multiinput.php to multiedit.php to handle multiedit.php

Not sure what went wrong but the update function is not working. I'm pretty sure there needs to be an ID passing from multiedit.php to handlemultiedit.php . PS I'm new to php so please enlighten me on the mistakes and improvement. My intention is for the admin to multi select users using the checkboxes and click the submit button, which brings to their working schedule for the admin to edit them.

Checkbox is little tricky in form submitting. Let me explain what happens when checkbox are used. If the checkbox is unchecked the checkbox is treated as even not posted although the form is submitted. But in case of text box if the textbox is empty null textbox will be posted on submit. so in this case the unchecked array elements of the array checkbox2[] wont be submitted, shifting the array values only with the checked values. this makes the results wrong.

Example: here is your form values

checkbox2[0] is checked // this will be submitted
checkbox2[1] is unchecked //this wont be submitted
checkbox2[2] is checked // this will be submitted

when the form is posted, data will look like this. wrong & not as expected

checkbox2[0]=checked
checkbox2[1]=checked

So the way to use multiple checkbox you could use a hidden textbox for each checkbox with javascript onchange to manupulate the hidden textbox value according to onchange or checked event of the corresponding checkbox.

[EDIT] check this code

Additional Note: for debugging purpose add following php code to multiedit.php to see the submitted values

if($_POST){
echo '<pre>';
print_r($_POST);
echo '</pre>';

Edit the following section in your code like this

$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++;
                } 
}

Add following jquery to the bottom of the page. Remember to add jquery.js file to the 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>

Additional: Do not conside below part as part of the answer

Hope this helps. i have tested and it works fine.

For additional reference, here is the test.php that i used for testing, for better understanding

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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