好吧所以我正在做一个mysql选择,如果没有结果那么我们将插入它如果有结果我们更新它但我得到了 解析错误:语法错误,意外T_VARIABLE
这是我的完整代码
$query = mysql_query("SELECT * from new_battles WHERE player1='".$_SESSION['username']."'");
if(mysql_num_rows($query)!=0)
{# If row already exists, then update it.
$result2 = mysql_query("UPDATE new_battles SET player1 = '"$_SESSION['username']"' AND player2 = '"$_SESSION['vsuser']"'
AND onpokemonplayer1 = '1' AND onpokemonplayer2 = '1' WHERE player1 = '"$_SESSION['username']"' ")
or die(mysql_error());
}
else
{# If row doesn't exist, then insert row.
$result = mysql_query("INSERT INTO new_battles (player1, player2,onpokemonplayer1,onpokemonplayer2) VALUES('"$_SESSION['username']"', '"$_SESSION['vsuser']"' , '1', '1')")
or die(mysql_error());
}
它说错误是在mysql更新位上...也许我正在使用许多和??? ???
答案 0 :(得分:2)
您忘记了.
在查询中连接变量:
$result2 = mysql_query("UPDATE new_battles
SET player1 = '".$_SESSION['username']."',
player2 = '".$_SESSION['vsuser']."',
onpokemonplayer1 = '1',
onpokemonplayer2 = '1'
WHERE player1 = '".$_SESSION['username']."' ") or die(mysql_error());
编辑:正如评论中所提到的那样,UPDATE查询错误,要更新多个字段,需要用逗号分隔它们而不是AND。
答案 1 :(得分:1)
每个语法错误都必须处理string concatenation:
<?php
$query = mysql_query("SELECT * from new_battles WHERE player1='".$_SESSION['username']."'");
if(mysql_num_rows($query)!=0){# If row already exists, then update it.
$result2 = mysql_query("UPDATE new_battles SET player1 = '".$_SESSION['username']."' AND player2 = '".$_SESSION['vsuser']."'
AND onpokemonplayer1 = '1' AND onpokemonplayer2 = '1' WHERE player1 = '".$_SESSION['username']."' ")
or die(mysql_error());
}else{# If row doesnt exist, then insert row.
$result = mysql_query("INSERT INTO new_battles (player1, player2,onpokemonplayer1,onpokemonplayer2) VALUES('".$_SESSION['username']."', '".$_SESSION['vsuser']."' , '1', '1')") or die(mysql_error());
}?>