Home > Database > Mysql Tutorial > How Can I Increment a Value in a MySQL UPDATE Query?

How Can I Increment a Value in a MySQL UPDATE Query?

Mary-Kate Olsen
Release: 2024-12-27 01:03:09
Original
564 people have browsed it

How Can I Increment a Value in a MySQL UPDATE Query?

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]);
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template