Note that there are some explanatory texts on larger screens.

plurals
  1. POSyntax for ON DUPLICATE KEY UPDATE with positional placeholders
    primarykey
    data
    text
    <p>This is <code>REPLACE</code> syntax</p> <pre><code>$sql = "REPLACE INTO 2_1_journal (Number,RecordDay, RecordMonth) VALUES "; $insertQuery = array(); $insertData = array(); foreach ($_POST['row_id'] as $i =&gt; $row_id) { $insertQuery[] = '(?, ?, ?)'; $insertData[] = $row_id; $insertData[] = $_POST['date_day'][$i]; $insertData[] = $_POST['date_month'][$i]; } if (!empty($insertQuery)) { $sql .= implode(', ', $insertQuery); $stmt = $db-&gt;prepare($sql); $stmt-&gt;execute($insertData); } </code></pre> <p>However read that instead of <code>REPLACE</code> better use <code>ON DUPLICATE KEY UPDATE</code></p> <p>Trying to change like this</p> <pre><code>$sql = "INSERT INTO 2_1_journal (Number,RecordDay, RecordMonth) VALUES ON DUPLICATE KEY UPDATE (RecordDay, RecordMonth) "; $insertQuery = array(); $insertData = array(); foreach ($_POST['row_id'] as $i =&gt; $row_id) { $insertQuery[] = '(?, ?, ?, ?, ?)'; $insertData[] = $row_id; $insertData[] = $_POST['date_day'][$i]; $insertData[] = $_POST['date_month'][$i]; $insertData[] = $_POST['date_day'][$i]; $insertData[] = $_POST['date_month'][$i]; } if (!empty($insertQuery)) { $sql .= implode(', ', $insertQuery); $stmt = $db-&gt;prepare($sql); $stmt-&gt;execute($insertData); } </code></pre> <p>But does not work (neither inserts nor updates)</p> <p>What is incorrect?</p> <p>If downvote, please write in comments why (for me to avoid write things that cause downvote)</p> <p><strong>Update</strong></p> <p>Changed code to this</p> <pre><code>$sql = "INSERT INTO 2_1_journal (Number, RecordDay) VALUES ON DUPLICATE KEY UPDATE RecordDay"; $insertQuery = array(); $insertData = array(); foreach ($_POST['row_id'] as $i =&gt; $row_id) { $insertQuery[] = '(?, ?)'; $insertData[] = $row_id; $insertData[] = $_POST['date_day'][$i]; } </code></pre> <p>get SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON DUPLICATE KEY UPDATE RecordDay('21', ''), ('22', '')' at line 1</p> <p>Changed to this</p> <pre><code>$sql = "INSERT INTO 2_1_journal (Number, RecordDay) VALUES ON DUPLICATE KEY UPDATE RecordDay=VALUES(Number)"; $insertQuery = array(); $insertData = array(); foreach ($_POST['row_id'] as $i =&gt; $row_id) { $insertQuery[] = '(?, ?)'; $insertData[] = $row_id; $insertData[] = $_POST['date_day'][$i]; </code></pre> <p>get SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON DUPLICATE KEY UPDATE RecordDay=VALUES(Number)('21', ''), ('22', '')' at line 1</p> <p>Changed code to this</p> <pre><code>$sql = "INSERT INTO 2_1_journal (Number, RecordDay) VALUES ON DUPLICATE KEY UPDATE RecordMonth=?"; $insertQuery = array(); $insertData = array(); foreach ($_POST['row_id'] as $i =&gt; $row_id) { $insertQuery[] = '(?, ?, ?)'; $insertData[] = $row_id; $insertData[] = $_POST['date_day'][$i]; $insertData[] = $_POST['date_month'][$i]; </code></pre> <p>get SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens</p> <p>Can I make conclusion that last example is valid SQL syntax. But why error? Number, RecordDay, RecordMonth=? and <code>$insertQuery[] = '(?, ?, ?)';</code> 3 variables and 3 tokens? Or I am wrong?</p> <p>If <code>$insertQuery[] = '(?, ?)';</code> the same SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens</p> <p>Please, help. I am stuck.... no idea</p> <p><strong>Working code!!!</strong></p> <pre><code>try { $sql = "INSERT INTO 2_1_journal (Number, RecordDay) VALUES "; $insertQuery = array(); $insertData = array(); foreach ($_POST['row_id'] as $i =&gt; $row_id) { $insertQuery[] = '(?, ?)'; $insertData[] = $row_id; $insertData[] = $_POST['date_day'][$i]; } if (!empty($insertQuery)) { $sql .= implode(', ', $insertQuery) . 'ON DUPLICATE KEY UPDATE RecordDay = VALUES(RecordDay);'; $stmt = $db-&gt;prepare($sql); $stmt-&gt;execute($insertData); } } catch (PDOException $e){ echo "DataBase Error: " .$e-&gt;getMessage() .'&lt;br&gt;'; } catch (Exception $e) { echo "General Error: ".$e-&gt;getMessage() .'&lt;br&gt;'; } </code></pre> <p>Your comments, please. And see location of <code>ON DUPLICATE KEY UPDATE</code>!!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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