Incrementing Values in MySQL Update Queries
While attempting to add a point to a user's existing points in a database using an update query, you encounter an issue where the points are incorrectly set to 1 instead of the intended incremented value.
To rectify this, rather than manually concatenating the value and the increment, you need to directly increment the value that already exists in the database. The corrected query using prepared statements for both PDO and mysqli would look like this:
$sql = "UPDATE member_profile SET points = points + 1 WHERE user_id = ?"; $db->prepare($sql)->execute([$userid]);
This ensures that the points are properly incremented, avoiding the incorrect value assignment issue.
The above is the detailed content of How Can I Increment a Value in a MySQL UPDATE Query?. For more information, please follow other related articles on the PHP Chinese website!