I’m trying to write a query to insert/update a table and it’s my first time using prepared statements, I’m receiving the error
SQLSTATE[HY093]: Invalid parameter number but from what I can see I’m passing all the columns/values required.
(I’m trying to do this without using
bindParam as in example #2 here)
This is just a test for now, I plan on making it dynamic once I’ve actually got a query working.
$data_test = [ ':event_id' => 3354, ':event' => 'TESTESTEST', ':staff_booking_id' => 27255, ':is_read' => 'yes', ':priority' => 'medium' ]; $q = "INSERT INTO events(event_id, event, staff_booking_id, is_read, priority) VALUES(:event_id, :event, :staff_booking_id, :is_read, :priority) ON DUPLICATE KEY UPDATE event_id = LAST_INSERT_ID(:event_id), event = :event, staff_booking_id = :staff_booking_id, is_read = :is_read, priority = :priority;"; $result = $this->db->prepare($q); $result = $result->execute($data_test);
As commentented by FunkFortyNiner and tadman, it is possible that the issue comes from the fact that you are reusing the same placeholder.
Actually the MySQL syntax does not require you to reuse the named parameter: you can use the
VALUES() to refer to the values initially passed for
Also, your attempt to update
LAST_INSERT_ID() does not seem right; I am unsure that this is valid syntax – and anyway, if this is the primary key of table, then you don’t want to update it.
Finally, as pinpointed by FunkFortyNiner,
event is a reserved word in MySQL, so it needs to be quoted.
$q = "INSERT INTO events( event_id, `event`, staff_booking_id, is_read, priority ) VALUES( :event_id, :event, :staff_booking_id, :is_read, :priority ) ON DUPLICATE KEY UPDATE `event` = VALUES(`event`), staff_booking_id = VALUES(staff_booking_id), is_read = VALUES(is_read), priority = VALUES(priority)";