INSERT INTO `max_score`
SELECT 77, 7 FROM `max_score` WHERE NOT EXISTS
(SELECT * FROM `max_score` WHERE id = 77 AND score >= 7) LIMIT 1;
(77
is the id
, and 7
is the score
.)
This will insert if there are no values greater than or equal to 7 with and id equal to 77, and will NOT insert if there are values greater than 7.
And with prepared statements and a timestamp:
<?php
$id = 77;
$score = 13;
$date = new DateTime();
$timestamp = $date->format('Y-m-d H:i:s');
$sql = <<<EOT
INSERT INTO `max_score`
SELECT ?, ?, ? FROM `max_score` WHERE NOT EXISTS
(SELECT * FROM `max_score` WHERE id = ? AND score >= ?) LIMIT 1;
EOT;
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('iisii', $id, $score, $timestamp, $id, $score);
$stmt->execute();
?>
(One caveat: table max_score
must have at least one record in it for the SELECT ?, ? FROM
max_score` to return the provided values.)
Inspired by: MySQL: Insert record if not exists in table