我有这个PHP代码:
<?php
$db = mysqli_connect("*****","*****","******") or die('Failed to connect: ' . mysql_error());
$playerName = mysqli_real_escape_string($db,$_GET['name']);
$playerScore = mysqli_real_escape_string($db,$_GET['score']);
$mysqlCommand = "USE scoreboard1;
INSERT INTO Scores
SET name = '$playerName'
, score = '$playerScore'
, ts = CURRENT_TIMESTAMP
ON DUPLICATE KEY UPDATE
ts = if( $playerScore > score , CURRENT_TIMESTAMP , ts)
, score = if( $playerScore > score , $playerScore , score);";
$resault = mysqli_query($db,$mysqlCommand) or die ("Command Failed , " . mysqli_error($db));
?>
但是当我运行代码(myhost.com/addscore.php?name=Peyman%score=10)时,出现此错误:
Command Failed , 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 'INSERT INTO Scores SET name = 'Peyman' , score = '10' , ts ' at line 2
((PHP 7.3.6,MySql 5)
帮助我):
谢谢@barmar,@JohnConde我的问题通过在mysqli_connect的第四个参数中写入数据库名称并删除USE scoreboard1来解决。从mysqli_query。编辑的PHP文件:
<?php
$db = mysqli_connect("*****","*****","******","scoreboard1") or die('Failed to connect: ' . mysqli_error($db));
$playerName = mysqli_real_escape_string($db,$_GET['name']);
$playerScore = mysqli_real_escape_string($db,$_GET['score']);
$mysqlCommand = "INSERT INTO Scores
SET name = '$playerName'
, score = '$playerScore'
, ts = CURRENT_TIMESTAMP
ON DUPLICATE KEY UPDATE
ts = if( $playerScore > score , CURRENT_TIMESTAMP , ts)
, score = if( $playerScore > score , $playerScore , score);";
$resault = mysqli_query($db,$mysqlCommand) or die ("Command Failed , " . mysqli_error($db));
?>