Yohan Blake
Yohan Blake

Reputation: 1318

Table loaded from database won't send correct data via POST

I am loading a table from a mysql database. I have included in a form since I want users to be able to make changes to the database and submit. The data loads from the db fine, but when I change a value and hit submit, it always sends the data of the last row of the table, and not the value I changed. How can I send the whole table to the db? The $stid is correct.

<?php 
$q3 = mysqli_query($link,"SELECT * FROM stores_op_hours WHERE Store_Id='$stid'
order by field(Day_Name,'sun','mon','tue','wed','thu','fri','sat')" );

?>
<form action="store-admin.php" method="post">
<table class="table ophours">
    <thead class="thead-inverse">       
        <tr><th>Day</th><th>Open Time</th><th>Close Time</th><th>Notes</th></tr>
    </thead>
    <tbody>
<?php 

while($rw = mysqli_fetch_array($q3)){

    $day = $rw['Day_Name'];
    $op = $rw['Open_Time'];
    $cl = $rw['Close_Time'];
    $day2 = strtoupper($day);
    $notes = $rw['Store_Op_Notes'];
?>  
        <input type="hidden" name="day" value="<?php echo $day; ?>" >
        <input type="hidden" name="stid" value="<?php echo $stid; ?>" >
        <tr><td><input type="text" readonly name="day2" value="<?php echo $day2; ?>" ></td>
        <td><input type="text" name="open" value="<?php echo $op; ?>" > </td>
        <td><input type="text" name="close" value="<?php echo $cl; ?>" > </td>
        <td><input type="text" name="notes"<?php echo $notes; ?> ></td></tr>

<?php
}
?>
</tbody>
</table>
<input type="submit" id="ophourchange" name="ophourchange" value="Save Changes" >
</form>

The POST code:

if(isset($_POST['ophourchange'])){
    $stid = $_POST['stid'];
    $day = $_POST['day'];
    $open = $_POST['open'];
    $close = $_POST['close'];
    $notes = $_POST['notes'];
    echo "<script type='text/javascript'>alert('stid". $stid."')</script>";
    echo "<script type='text/javascript'>alert('day". $day."')</script>";
    echo "<script type='text/javascript'>alert('open". $open."')</script>";
    echo "<script type='text/javascript'>alert('close". $close."')</script>";
    echo "<script type='text/javascript'>alert('notes". $notes."')</script>";

    $upd_st_op = " UPDATE stores_op_hours SET Open_Time='$open', Close_Time='$close', Store_Op_Notes='$notes' WHERE Store_Id='$stid' AND Day_Name='$day' ";

    $res = mysqli_query($link, $upd_st_op);
    if (!mysqli_query($link,$res))
    {
        echo "<script type='text/javascript'>alert('Error". mysqli_error($link)."')</script>";
    }else{
        //echo "Store Updated";
        echo "<script type='text/javascript'>alert('Store Open Hours Updated')</script>";

    }
}

Upvotes: 1

Views: 62

Answers (3)

Harsh Sanghani
Harsh Sanghani

Reputation: 1712

Can you please change your while loop like following :-

<?php 
$i = 0;
while($rw = mysqli_fetch_array($q3)){

    $day = $rw['Day_Name'];
    $op = $rw['Open_Time'];
    $cl = $rw['Close_Time'];
    $day2 = strtoupper($day);
    $notes = $rw['Store_Op_Notes'];
?>  
    <input type="hidden" name="data[<?php echo $i;?>]['day']" value="<?php echo $day; ?>" >
    <input type="hidden" name="data[<?php echo $i;?>]['stid']" value="<?php echo $stid; ?>" >
    <tr>
        <td><input type="text" readonly name="data[<?php echo $i;?>]['day2']" value="<?php echo $day2; ?>" ></td>
        <td><input type="text" name="data[<?php echo $i;?>]['open']" value="<?php echo $op; ?>" > </td>
        <td><input type="text" name="data[<?php echo $i;?>]['close']" value="<?php echo $cl; ?>" > </td>
        <td><input type="text" name="data[<?php echo $i;?>]['notes']" value="<?php echo $notes; ?>" ></td>   
    </tr>

<?php
 $i++;
}
?>

Then You can test data with $_POST['data'];

<?php
   echo '<pre>';
   print_r($_POST['data']);
?>

It may help you

Upvotes: 0

Rasclatt
Rasclatt

Reputation: 12505

When you create your rows use your row id, assuming you have an auto-incrementing id column in your table:

while($rw = mysqli_fetch_array($q3)){
    // You need to have this column here
    $id    =  $rw['id'];
    $day   =  $rw['Day_Name'];
    $op    =  $rw['Open_Time'];
    $cl    =  $rw['Close_Time'];
    $day2  =  strtoupper($day);
    $notes =  $rw['Store_Op_Notes'];
?>  
        <input type="hidden" name="day[<?php echo $id; ?>]" value="<?php echo $day; ?>" >
        <input type="hidden" name="stid[<?php echo $id; ?>]" value="<?php echo $stid; ?>" >
        <tr><td><input type="text" readonly name="day2[<?php echo $id; ?>]" value="<?php echo $day2; ?>" ></td>
        <td><input type="text" name="open[<?php echo $id; ?>]" value="<?php echo $op; ?>" > </td>
        <td><input type="text" name="close[<?php echo $id; ?>]" value="<?php echo $cl; ?>" > </td>
        <td><input type="text" name="notes[<?php echo $id; ?>]"<?php echo $notes; ?> ></td></tr>
<?php
}

Then your sql statement (which you are just testing now but will fix all the injection issues later, no doubt) would have the id as another parameter to match:

"UPDATE `stores_op_hours`
    SET `Open_Time` = '$open',
        `Close_Time` = '$close',
        `Store_Op_Notes` = '$notes'
    WHERE
        `id` = '{$id}'";

Upvotes: 1

Arasan
Arasan

Reputation: 137

give each input a name like

<input type="hidden" name="data[0][day]" value="" />
<input type="hidden" name="data[0][stid]" value="" />

<input type="hidden" name="data[1][day]" value="" />
<input type="hidden" name="data[1][stid]" value="" />

then in php

$_POST['data'];

will be an array

Upvotes: 0

Related Questions