gpt4 book ai didi

php - 使用 PHP 将数字添加到 MySql 值

转载 作者:行者123 更新时间:2023-11-29 23:31:13 26 4
gpt4 key购买 nike

我使用以下 PHP 将 MySql 数据库中的表值加 1,并在每次执行代码时更新数据库。

function create_update() {
$user_id = $_SESSION['user_id'];
$con = mysqli_connect(HOST, USER, PASSWORD, DATABASE);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$numberofupdates = number_of_updates(); //'number_of_updates()' gets an INT from the database
settype($numberofupdates, "integer"); //set the number to an INT if it already is not
$spare_update_id = $numberofupdates + 1; //get number and add 1
mysqli_query($con,"UPDATE members SET updates=$spare_update_id WHERE id = $user_id"); //update the value to the database
}

代码工作正常并更新到数据库,但似乎每次都会将值更新为 1。如果我手动将数据库值设置为 4 并运行代码,它会将该值设置回 1,而该值应该是 5。

感谢任何帮助!

最佳答案

如果我正确理解你的问题,你只需要 1 个 mysql 查询

function create_update() {
$user_id = $_SESSION['user_id'];
$con = mysqli_connect(HOST, USER, PASSWORD, DATABASE);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"UPDATE members SET updates = (updates + 1) WHERE id = $user_id");
}

关于php - 使用 PHP 将数字添加到 MySql 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26569327/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com