I'm new to databases and I need your help I'm working on a website and I have few problems. I'm using PDO. There is a user table, with unique ID, name and password. User enters his username and password and is put to the next page where he enters time, date and a third parameter. Those 3 parameters are put into anoter database with user's ID (ID is set with PHP's session). This table has no primary key, since user can insert multiple entries. Sounds great, but... I want to delete duplicates, or even better prevent user to insert a duplicate. I tried with this:
$alter = "ALTER IGNORE TABLE data ADD UNIQUE INDEX (ring_time, ring_date, bell_mode)";
$query = $db->prepare($alter);
$query->execute();
try {
$insert_s = "INSERT INTO data(id, ring_time, ring_date, bell_mode) VALUES(?, ?, ?, ?)";
$query = $db->prepare($insert_s);
$query->execute(array($_SESSION['id'], $_POST['ring_time'], $_POST['ring_date'], $_POST['mode']));
} catch(PDOException $e) {
echo "<p class='already_entered'>Already entered.</p>"
}
This works somehow right, but if different user inserts same values, it catches the error, even though his ID is different. I'm pretty sure that $alter statement is wrong. The other thing is: I want to delete every entry which date (the table's date entry is 'date' type) is past today's day. I tried with:
DELETE FROM data WHERE CURDATE() > ring_date";
but with no success, it deletes stuff, but not the right way.