1

好的,所以我在做一个 mysql 选择,如果没有结果,那么我们将插入它,如果有结果我们更新它,但我收到 Parse 错误:语法错误,意外的 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更新位上......也许我正在使用很多and's ???

4

2 回答 2

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 分隔它们。

于 2012-12-09T00:02:46.703 回答
1

每个语法错误都必须处理字符串连接

<?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());
}?>
于 2012-12-09T00:03:42.110 回答