我正在尝试为我的论坛创建一个函数,它会将我的用户的“帖子”属性增加1.无论出于何种原因,以下PHP都不起作用。
function postCountIncrease($username) {
//get the connection variable
global $con;
//change to the users database (this function works correctly)
sqlconnect_users();
//get current post number (this is also working)
$getCurrentPosts = "SELECT Posts\n"
. "FROM users\n"
. "WHERE Username='".$username."'";
$query1 = mysqli_query($con, $getCurrentPosts) or die(mysqli_error($con));
$currentPosts = mysqli_fetch_array($query1);
//here is the problematic post. Assume that $username is a valid value, and that I've already done mysqli_real_escape_string() on it
$incrementPostsQuery = "UPDATE users.users SET Posts=". $currentPosts[0]+1 ." WHERE Username='". $username ."'";
$query2 = mysqli_query($con, $incrementPostsQuery) or die(mysqli_error($con));
//return the result
$result = mysqli_fetch_array($query2);
return $result;
}
老实说,我看不出我做错了什么,因为SQL运行正常。如果我在控制台中使用UPDATE users.users SET Posts=1 WHERE Username='Lampitosgames'
,则无错误。帮助很多。此外,这是它向我投掷的错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1 WHERE Username='Lampitosgames''
答案 0 :(得分:1)
你无法以"toto ".$var+1
的方式连接,你必须用括号"toto ".($var+1)
在您的情况下,这是var $incrementPostsQuery
的声明失败
答案 1 :(得分:0)
查看您的错误,您的语法已关闭
$getCurrentPosts = "SELECT Posts
FROM users
WHERE Username='$username'";
答案 2 :(得分:0)
错误在于构建查询。
$incrementPostsQuery = "UPDATE users.users SET Posts=". $currentPosts[0]+1 ." WHERE Username='". $username ."'";
我会建议您创建这样的查询提示: