简体   繁体   中英

Multiple Mysql Updates in one query

I am trying to update the same field over different rows using PDO and MYSQL , I generate a table with a statement and then pass the id & "result" (status update) to php, the following is the code for my table, but when I go to update, first it tells me there are to many bind params even when I input a value for all fields generated by the table and it still wont update the table!

<form action='' method='POST'>
<table width="auto" class="table table-bordered table-hover">
    <tr>
<th class='col-md-1'>ID</th>
<th class='col-md-1'>User ID</th>
<th class='col-md-1'>Name</th>
<th class='col-md-1'>Date submited</th>
<th class='col-md-1'>Date Requested</th>
<th class='col-md-1'>Status</th>
</tr>

<?php
$sql = "SELECT * FROM data";
$sth = $pdo->query($sql);
$count= $sth->rowCount();
while($row = $sth->fetch()) {

echo "<tr>";
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['user_id']. "</td>";
echo "<td>" . $row['Name']. "</td>";
echo "<td>" .$row['Date_submited']. "</td>";
echo "<td>" .$row['Date_requested']. "</td>";
echo "<td>" .$row['Status']. "</td>";
echo "<td> <select name='Status[]' value=''> <option disabled selected='true'>Please     Select</option>  <option value='denied'>Denied</option> <option value='approved'>Approved</option> </td>";
echo "<input type='hidden' name='id[]' value='".$row['id']."' />";
echo "</tr>";
}

?>
<td>      <input type='submit' name='approve' class='btn btn-default' value='submit'/></td>
 </table>
 </form>

And this is my update query:

if (isset( $_POST['approve'] ) ){
$result = $_POST['Status'];
$id = $_POST['id'];
$a = count($id);
try{
$sth = $pdo->prepare('UPDATE data 
                                SET 
                                Status = :Status
                                                WHERE id = :id              
                                ');
$i = 0; 
while ($i < $a) {
    $sth->bindParam(':Status', $result[$i]);
    $sth->execute();
    $i++;
    }
    }
    catch(PDOException $e) {  
    echo "I'm sorry, but there was an error updating the database.";  
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}
}

You have the loop in the wrong place and you need to bind all of your query parameters -

if (isset( $_POST['approve'] ) ){
    // loop through results of post array
    for($i = 0; $i < count($_POST['id']); $i++) {
        $result = $_POST['Status'][$i];
        $id = $_POST['id'][$i];
        try{
            $sth = $pdo->prepare('UPDATE data SET Status = :Status WHERE id = :id');
            $sth->bindParam(':Status', $result);
            $sth->bindParam(':id', $id);
            $sth->execute();
        }
        catch(PDOException $e) {  
            echo "I'm sorry, but there was an error updating the database.";  
            file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
        }
    }
}

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