如果记录存在2列的值,则忽略insert

I'm trying to modify this so that if someone tries to insert a record that already exists with the pageID and displayID, it will simply ignore the INSERT and reload the page, effectively making no change.

I have a unique index on my page_id and display_id columns but it still inserts duplicates here.

How can I fix this to properly keep from inserting duplicates and just ignoring the insert request?

$pageID = $_GET['pageID'];
$displayID = $_GET['displayID'];

$assignPage = "
INSERT IGNORE INTO display_to_page(page_id, display_id)
Values( '".$pageID."', '".$displayID."')
";

$performAssign = $mysqlConn->query($assignPage);


header("Location: displayPage.php?displayID=".$displayID);
exit;

If you have a unique index on both columns, then duplicates should not be inserted into the table.

This would look like either unique(page_id, display_id) in the table definition (or similar constraint) or:

create unique index unq_display_to_page_2 on display_to_page(page_id, display_id);

Note that these constraints/indexes are on the display_to_page table.

With such an index/constraint in the database, MySQL will not allow duplicates into the table.

You should also check the ids being passed into the string. In fact, you should switch to using parameters. Perhaps bad characters are getting into the query string somehow.