0

I'm trying to edit multiple entries at once using a loop and the UPDATE function with a handle. However the entries go through correctly but database doesn't change. This code works fine when I'm doing single edit.

<?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) && crew_id";
}

$result = mysqli_query($con, $queryStr);


    $checkbox2 = $_POST['checkbox2'];
    foreach ($checkbox2 as $crewname) {

        ?> <form action="handlemultiedit.php" method="post">
            <input type="hidden" name="crew_id" value="<?php echo $id = isset($_GET['id']) ? $_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>";
        ?><?php
    }?><td><input type="submit" value="Submit" ></td></form><?php

?>

This is the handle page

<?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)) {
$sqlQueryStr = "UPDATE crewlist SET start_hour = '$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);
?>
forumzeon1
  • 55
  • 3
  • Your start tag for the form, is inside the loop, while your closing form tag, is outside it, which means that you get `
    `, either move the ending form tag inside of the foreach loop or move the start tag of the form out of the foreach loop
    – Epodax Oct 02 '15 at 06:41
  • This may or may not apply. I don't use `mysqli` but it sounds similar: http://stackoverflow.com/questions/614671/commands-out-of-sync-you-cant-run-this-command-now although those are select...but you can decide if it applies. – Rasclatt Oct 02 '15 at 06:43
  • tag should be out of the loop. – Suman Singh Oct 02 '15 at 06:47
  • I changed the form tag to outside of the loop but the problem is still there. – forumzeon1 Oct 02 '15 at 06:50
  • check the print_r($_POST); and if you got the result than check the sql query and try to run it on phpmyadmin directly. – Suman Singh Oct 02 '15 at 06:55
  • print_r($_POST) shows : Array ( [crew_id] => [start_hour] => 23:30 [end_hour] => 11:00 [start_hour2] => 12:30 [end_hour2] => 12:30 ) but the database doesn't change – forumzeon1 Oct 02 '15 at 06:58

0 Answers0