Whisou138
Whisou138

Reputation: 481

update on duplicate key in php script

I've created a unique index on my table for page_id and display_id but i'm having issues with PHP syntax on doing a duplicate key update call.

This is the sql query in my php script:

$assignPage = "
INSERT INTO display_to_page (page_id, display_id)
VALUES ( '".$pageID."', '".$displayID."')
ON DUPLICATE KEY UPDATE
active = 1
";

But it's not passing the syntax at the ON DUPLICATE KEY part.

I just want to say (insert this,but if the page_id/display_id key exists then update by setting the active column to 1)

Am I doing something wrong here?

Upvotes: 0

Views: 88

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94662

The string concatenation had an error, so I simplified it.

$assignPage = "INSERT INTO display_to_page (page_id, display_id)
                VALUES ( '$pageID', '$displayID')
                ON DUPLICATE KEY UPDATE active = 1";

You should also use prepared parameterized statements in either the MYSQLI_ or PDO API's rather than value concatenation.

Upvotes: 1

Related Questions