开发者

Trouble updating a particular user in a mysql database

im trying to update a particular user that is logged in using UPDATE mysql command, but instead it is going to the first user that is in the database itself, if you can help id appreciate it

Edit: Im wanting to increment the number of 'items' that a user has, but for the code below its only going to the first user in the database

<?php
session_start();
        $dbhost = 'localhost';
        $dbuser = '';
        $dbpass = '';
        $dbname = '';
        $conn = mysql_connect($dbhost,$dbuser,$dbpass)
            or die ('Error connecting to mysql');
        mysql_select_db($dbname);
            $query = sprintf("UPDATE users SET item = item + 1 ",
        开发者_StackOverflow中文版mysql_real_escape_string($_POST['item']));
            mysql_query($query);
        ?>


Your sprintf() call has a parameter, but no placeholder:

$query = sprintf("UPDATE users SET item = item + 1 ",
      mysql_real_escape_string($_POST['item']));

Probably this is supposed to be something like the following, assuming an INT column named item

$query = sprintf("UPDATE users SET item = item + 1 WHERE item = %d ",
    mysql_real_escape_string($_POST['item']));

UPDATE

If you are trying to target a specific user only, then you need that user's id or username in $_POST instead of item. You'll need to post the output of var_dump($_POST) for us to see just what values you've received in post.

Assuming a string username, use:

$query = sprintf("UPDATE users SET item = item + 1 WHERE username = '%s' ",
    mysql_real_escape_string($_POST['username']));


you need some kind of where clause. specify which user you want to actually update with extra conditions.


YOu need to know which user you want to update...

$query = sprintf("UPDATE users SET item = item + 1 WHERE userId="+ $userId,

or something like that...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜