gpt4 book ai didi

php - MySQL中的行值增加和减少1

转载 作者:IT老高 更新时间:2023-10-28 12:59:21 25 4
gpt4 key购买 nike

您好,我有一个 MySQL 数据库表“点”,用户可以单击一个按钮,并且应该从他们的帐户中删除一个点,他们按下的按钮具有另一个用户的 ID,因此他们的帐户必须增加一个。

我让它在 jQuery 中工作并检查了 Firebug 中的变量/帖子,它确实发送了正确的数据,例如:

userid= 1 
posterid = 4

我认为问题出在我的 PHP 页面上:

<?php


include ('../functions.php');

$userid=mysql_real_escape_string($_POST['user_id']);
$posterid=mysql_real_escape_string($_POST['poster_id']);

if (loggedin())
{
include ('../connection.php');
$query1 = "UPDATE `points` SET `points` = `points` - 1 WHERE `userID` = '$userid'";
$result1=mysql_query($query1);


$query2 = "UPDATE `points` SET `points` = `points` + 1 WHERE `userID` = '$posterid'";
$result2=mysql_query($query2);


if ($result1 && result2)
{
echo "Successful";
return 1;
}
else
{

echo mysql_error();
return 0;
}
}
?>

有什么想法吗?谢谢:)

最佳答案

不需要增加/减少字段值的两个查询:

mysql_query("UPDATE table SET field = field + 1 WHERE id = $number");

是一个完全有效的查询,如下所示:

mysql> describe points;
+--------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+-------+
| uid | int(11) | NO | PRI | NULL | |
| points | int(11) | YES | | 0 | |
+--------+---------+------+-----+---------+-------+
2 rows in set (0.05 sec)

mysql> insert into points VALUES (1,0),(2,0);
Query OK, 2 rows affected (0.14 sec)

mysql> select * from points;
+-----+--------+
| uid | points |
+-----+--------+
| 1 | 0 |
| 2 | 0 |
+-----+--------+
2 rows in set (0.05 sec)

mysql> update points set points = points+1 where uid = 1;
Query OK, 1 row affected (0.27 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from points;
+-----+--------+
| uid | points |
+-----+--------+
| 1 | 1 |
| 2 | 0 |
+-----+--------+
2 rows in set (0.00 sec)

经过测试,你确定你进入了 if (loggedin()) 子句吗?

我必须同意 KM,很高兴看到 echo $query1;echo $query2;

关于php - MySQL中的行值增加和减少1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2834866/

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