search

Home  >  Q&A  >  body text

Update multiple rows of data using PDO loop

I have a form that retrieves multiple rows of data, each item has a text area for users to comment on a specific item. The number of items returned is variable, and they do not have to leave comments in any/all boxes.

<textarea name="comment[]" cols="25" rows="2"><?php echo $f2; ?></textarea>
    <input name="tableid[]" type="hidden" value="<?php echo $f1; ?>">

The echo statement fills the text area with whatever is currently stored in the database, because the user can modify what others have entered.

When it is passed to the form processing page, it will return this..

Submit: Submit
    comment: Test Comment 1,Test Comment 2
    tableid: 590,591

So it seems to be passing the array correctly. I am using this code to update database

$conn = new PDO("mysql:host=xxxx;dbname=xxxxx",$username,$password);

$i = 0;
if(isset($_POST['submit'])) {
    foreach($_POST['comment'] as $comment) {
                        $comment = $_POST['comment'][$i];

            $id = $_POST['tableid'][$i];
            $stmt = $conn->prepare("UPDATE reservations SET comment=:comment WHERE     tableid=:id");

            $stmt->bindValue(':comment', $comment, PDO::PARAM_INT);
            $stmt->bindValue(':id', $id, PDO::PARAM_INT);

            $stmt->execute();

            $i++;
    }
}

However, this doesn't seem to be updated at all, where did I go wrong?

Thank you so much

P粉384366923P粉384366923417 days ago828

reply all(2)I'll reply

  • P粉704066087

    P粉7040660872023-10-25 09:13:24

    A few things:

    1. Set PDO to throw PDOException when an error occurs. This will make debugging easier.
    2. The point of a prepared statement is that you can call it multiple times with different variables, so that you only need to prepare it once and then call it multiple times. You can get a nice performance boost from it too.

    Code:

    $conn = new PDO("mysql:host=xxxx;dbname=xxxxx", $username, $password, [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //Set PDO to fire PDOExceptions on errors.
            PDO::ATTR_EMULATE_PREPARES => false //Disable emulated prepares. Solves some minor edge cases.
        ]);
    
    //No need for incrementer. The index of the comment should be enough.
        if (isset($_POST['submit'])) {
            //Note the prepare on the outside.
            $stmt = $conn->prepare("UPDATE `reservations` SET `comment` = :comment WHERE `tableid` = :id");
            //As well as the binding. By using bindParam, and supplying a variable, we're passing it by reference.
            //So whenever it changes, we don't need to bind again.
            $stmt->bindParam(":comment", $comment, PDO::PARAM_STR);
            $stmt->bindParam(":id", $id, PDO::PARAM_INT);
    
            foreach ($_POST['comment'] as $index => $comment) {
    
                //All that's left is to set the ID, see how we're reusing the $index of the comment input?
    
                $id = $_POST['tableid'][$index];
    
                $stmt->execute();
    
            }
        }

    reply
    0
  • P粉994092873

    P粉9940928732023-10-25 00:17:28

    <textarea name="comment[<?=$f1?>]" cols="25" rows="2"><?=$f2?></textarea>
    
    <?php
    $dsn = "mysql:host=xxxx;dbname=xxxxx";
    $opt = array(
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    );
    $conn = new PDO($dsn, $username, $password, $opt);
    
    $sql  = "UPDATE reservations SET comment=? WHERE tableid= ?";
    $stmt = $conn->prepare($sql);
    
    foreach ($_POST["comment"] as $id => $comment) {
        if ($comment) {
            $stmt->execute([$comment, $id]);
        }
    }

    reply
    0
  • Cancelreply