0

I have a database in MySql with two columns, one where the user's name is stored and the user's corresponding value in the second column. How do I replace the values in the second column with new values? I am having trouble finding the answer in Google, because I am not so familiar with PHP. I know that both REPLACE and UPDATE functions are there, but I am having trouble getting them to work for only that specific user, not ALL the values in the column. The code I am unsuccessfully using is this:

$query1 = "UPDATE TextMeBabe SET '$username' = replace( '$username', '$existingcredits', '$newcredits')";
    echo "Values added";
    mysql_query($query1);
    mysql_close();

(I am able to log into the database with no issues, so I have not provided that code)

4

2 回答 2

4
$query1 = "UPDATE TextMeBabe SET `credits`='$newcredits' WHERE (`username` ='$username')";

假设这些是您的字段名称。

于 2012-04-12T03:44:18.407 回答
0

1)您需要使用“where”子句来更新特定行。你可以在这里找到一些示例代码:http: //www.w3schools.com/php/php_mysql_update.asp

您这样做的方式将更新表中的所有记录。

2) replace 函数不适用于替换列中的值,它纯粹是一个字符串操作,用于替换作为第一个参数传递的部分字符串。

您正在考虑 repalce 命令(http://dev.mysql.com/doc/refman/5.0/en/replace.html)而不是替换功能(http://dev.mysql.com/doc/refman/ 5.1/en/string-functions.html#function_replace)

在问题的示例代码中,它应该是:

$query1 = "Update TextMeBabe Set credits = $newCredits where username = '$username'"; 假设列名是表中的用户名和信用。

于 2012-04-12T03:53:14.060 回答