Learning Mysql and having piles of questions...
I can achieve desired effect by simple:
$mysqli->query("INSERT results SET user_id = '".$user_data[0]['user_id']."', logo_id = '".$mysqli->real_escape_string($_GET['logo_id'])."'");
$mysqli->query("UPDATE results SET result_tries = result_tries +1 WHERE logo_id = '".$mysqli->real_escape_string($_GET['logo_id'])."' AND user_id = '".$user_data[0]['user_id']."'");
I've DB unique check
on the table, so that logo_id and user_id should be unique. So if row exists first query fails, and the second update is executed..., But this feels kind of hack... Is there a better way to do this?
Try
$user_id = $user_data[0]['user_id'];
$logo_id = $mysqli->real_escape_string($_GET['logo_id']);
$sql = "INSERT INTO results (user_id, logo_id, result_tries)
VALUES ('$user_id', '$logo_id', 0)
ON DUPLICATE KEY UPDATE result_tries = result_tries + 1";
$mysqli->query($sql);
In my case:
INSERT INTO assignment_status (user, assignment, status, note) VALUES (4, 7, 2, "My special note.")
ON DUPLICATE KEY UPDATE status=2, note="My special note."
Note that my primary key consists of 2 columns - user
and assignment
IDs...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.