简体   繁体   中英

Increase and decrease row value by 1 in MySQL

Hi I have a MySQL database table "points" the user can click a button and a point should be removed from their account, the button they pressed has an ID of another user, therefore their account must increase by one.

I have it working in jQuery and checked the varibles/posts in Firebug, and it does send the correct data, such as:

userid= 1 
posterid = 4

I think the problem is with my PHP page:

<?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;   
}
}
?>

Any ideas? Thanks :)

Two queries to increase/decrease field value are not necessary:

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

is a perfectly valid query as you can see next:

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)

Having that tested, are you sure you get into your if (loggedin()) clause?

I have to agree with KM , would be nice to see output of echo $query1; or echo $query2;

以下是我测试的示例查询,它正在100%工作

$query="UPDATE table_name SET `hit_count`=(`hit_count`+1) WHERE `id` = '1'";
update table_name set  col_name=col_name+1   where sqId = 12

但是,如果您的col_name默认值为null或为空,则它永远不会起作用,因此请确保col_name默认值为0或任何整数值。

try adding in something to print out your actual SQL command, before the if ($result1 && result2) :

ECHO '$query1='.$query1.'<br>';
ECHO '$query2='.$query2.'<br>';

this will help to see what it is sending to the database

update 'tablename' set 'columnname1'='columnname1' + 1 where 'columnname2'='value';

eg: update students set englishmarks=englishmarks + 1 where name='Rahul';

Edit:(Explanation) "UPDATE" keyword is used to update a vaule in the table. Here I am updating a value in the table "students". "SET" keyword updating the english marks by 1(just like in C language, how we increase the value of an integer, i=i+1) and condidtion is given where name is "Rahul".

So englishmarks of Rahul are incremented by 1

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM