user3154948
user3154948

Reputation: 137

Wrong DB record being updated

Maybe I'm tired, maybe I'm just going about this wrong but what ever the case something isn't right.

When the checkbox is ticked the DB is updated, but the $id is ignored and only the first record with an empty pu_time column is updated. What is supposed to happen is when the checkbox is ticked the current time is inserted into the corresponding record with the id provided. I can echo the $id, but it is still ignored.

I have successfully made this work with a textbox....

$sql = "SELECT * FROM  dispatch WHERE driver = '' OR pu_time = '' OR do_time = ''";
    $result = mysql_query($sql);
            $i = 0;
        $color1 = "#2b2823";
        $color2 = "#333333";
        while($row = mysql_fetch_array($result)){
            $id = $row['id'];
            if($i%2 == 1) {$color = $color1;}
                else {$color = $color2;}

                echo "<tr style='background-color: $color'>";
                echo '<td><input name="id" type="hidden" value="'.$row['id'].'" />'.$row['call_time'].'</td>';
                echo "<td>".$row['start_address']."</td>";
                echo "<td>".$row['end_address']."</td>";
                echo "<td>".$row['phone']."</td>";
                echo "<td>".$row['puat']."</td>";
if (isset($row['pu_time']) === true && empty($row['pu_time']) === true){

                        echo '<td>
                             <form id = "pu_time">
                              <input name="pick_up_time" type="checkbox" value="" onchange="this.form.submit()" />
                            </form></td>';
                                if (isset($_GET['pick_up_time'])){
                                    $pick_up = date('g:ia');
                                    $update_p = "UPDATE dispatch SET pu_time = '{$pick_up}' WHERE id = '{$id}'";
                                    mysql_query($update_p,$con)
                                    or die;
                                    unset($_GET['pick_up_time']);
                                    echo '<script type="text/JavaScript">
                                            window.location.href = "new_index.php";
                                          </script>';
                                    }
                        }
                        else {
                            echo "<td>".$row['pu_time']."</td>";
                            }

Upvotes: 1

Views: 69

Answers (1)

Reza Mamun
Reza Mamun

Reputation: 6189

Just two changes will fix your problem:

Add the the row ID into a hidden field and then submit:

<?php echo '
<td><form id = "pu_time">
    <input name="pick_up_time" type="checkbox" value="" onchange="if(this.checked)this.form.submit()" />
    <input name="rowId" type="hidden" value="'.$id.'" />
</form></td>';

And of course, DB update portion will be outside the loop to avoid unnecessary DB call as we are redirecting to a new page (or same page):

if (isset($_GET['pick_up_time']) && !empty($_GET['rowId'])){ // must have id;
    $pick_up = date('g:ia');
    $rowId = $_GET['rowId']; //mark this!
    $update_p = "UPDATE dispatch SET pu_time = '{$pick_up}' WHERE id = '{$rowId}'";
    mysql_query($update_p,$con)
    or die;
    unset($_GET['pick_up_time']);
    echo '<script type="text/JavaScript">
        window.location.href = "new_index.php";
      </script>';
}

 //and then write your code what is currently in the first line: $sql = "SELECT * FROM  dispatch WHERE dr ...... and so;

Upvotes: 1

Related Questions