Dylan Cross
Dylan Cross

Reputation: 5986

PHP/MYSQL - Insert into ignore, and ignore a specified column

I need to be able to only insert a row if the data doesn't already exist, except for the date column, which would always be different, so it will always insert a new row even though it shouldn't.

my code:

$postID = $_POST['postID']; //example, 817
$userID = logged_in(); //example, 2
$date = time();

mysql_query("INSERT IGNORE INTO likes SET userID='$userID', postID='$postID', date='$date'");

I would like to be able to do something like:

mysql_query("INSERT IGNORE date INTO likes SET userID='$userID', postID='$postID', date='$date'");

Upvotes: 3

Views: 1109

Answers (1)

Ja͢ck
Ja͢ck

Reputation: 173562

If you have a UNIQUE constraint on userID and postID you can do whatever you're doing now, the date column would not get updated if there's already a record.

If you want to have the date column updated if the record does exist (for that particular user and post) you can use ON DUPLICATE KEY UPDATE date = '$date'.

Upvotes: 3

Related Questions