Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Few things:</p> <ol> <li>Set PDO to throw <code>PDOException</code>s on errors. It would make debugging a whole lot easier.</li> <li>The point of prepared statements, is that you can call it many times using different variables, that way, you only need to prepare it once, and call it multiple times. You gain a nice performance boost out of it too.</li> </ol> <p>Code:</p> <pre><code>//New try/catch block to catch PDOExceptions should they arise. try { $conn = new PDO("mysql:host=xxxx;dbname=xxxxx", $username, $password, [ PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION, //Set PDO to fire PDOExceptions on errors. PDO::ATTR_EMULATE_PREPARES =&gt; 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-&gt;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-&gt;bindParam(":comment", $comment, PDO::PARAM_STR); $stmt-&gt;bindParam(":id", $id, PDO::PARAM_INT); foreach ($_POST['comment'] as $index =&gt; $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-&gt;execute(); } } } catch (PDOException $e) { echo "Some sort of error has occurred! Here are the details! "; echo $e-&gt;getMessage(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload