Note that there are some explanatory texts on larger screens.

plurals
  1. POMysql ON DUPLICATE KEY error
    primarykey
    data
    text
    <p>I'm having a little trouble with duplicate keys. I'll start with my code.</p> <pre><code>// Prepare to select all liabilities $stmt = $con-&gt;query("TRUNCATE TABLE `Students`"); $stmt = $con-&gt;prepare("SELECT student_no, legal_first_name, legal_surname, oen_number FROM csv_students"); $stmt-&gt;execute(); $stmt-&gt;bind_result($sid, $fname, $lname, $oen); $stmt-&gt;store_result(); $updt = $con-&gt;prepare("INSERT INTO Students (Student_ID, Last_Name, First_Name, OEN) VALUES (?, ?, ?, ?) ON DUPLICATE KEY DELETE FROM `Students` WHERE OEN = ?") or die(mysql_error($con)); // Prepare to insert a new liability $updt-&gt;bind_param("ssss", $sid, $lname, $fname, $oen, $oen) or die("Not sending var"); // Bind variables to the result of the query while($stmt-&gt;fetch()){ $updt-&gt;execute() or die(mysqli_error($con)); // Execute the query } $updt-&gt;close() or die("Not closing after send"); // Close the statement $stmt-&gt;free_result(); $stmt-&gt;close() or die("Not closing after collection"); </code></pre> <p>So I'm trying to get the variables from the first table and insert them into the second, but there's about four entries per student (one for each class). Basically, I'm getting them all and then trying to say "on a duplicate key, delete the last entry that has the same oen number. It's giving me the error "Warning: mysql_error() expects parameter 1 to be resource, object given in /var/www/ride/admin_settings/csv/students/update.php on line 23" which is on the line where we see the ON DUPLICATE KEY piece of code. Anyone have any idea what's going on?</p>
    singulars
    1. This table or related slice is empty.
    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.
    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