PHP/MySQL更新代码

The*_*nja 1 php mysql database

我正在尝试从php调用行更新到mysql数据库.它失败但是当我尝试调用插入新行时,它的工作方式相同.

$result = mysql_query("INSERT INTO auth (username, password, studycode, description, server) VALUES ('$username', '$password', '$studycode', '$description', '$server')");
Run Code Online (Sandbox Code Playgroud)

但是这段代码失败了

$result = mysql_query("UPDATE auth SET username='$username', password='$password', studycode='$studycode', description='$description', server='$server' WHERE index='$id' LIMIT 1;");
Run Code Online (Sandbox Code Playgroud)

index是第一列及其表的键/ id.

编辑:好的所以我刚进入mysql管理员并尝试了我的代码将发送的确切命令来跟踪错误.

UPDATE auth SET username='username', password='password', studycode='ab9102y', description='test change', server='server2' WHERE index='5' LIMIT 1;
Run Code Online (Sandbox Code Playgroud)

给了我错误

#1064 - 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 'index='5' LIMIT 1' at line 1 
Run Code Online (Sandbox Code Playgroud)

Chr*_*tow 7

可能是索引的保留关键字问题.尝试:

$result = mysql_query("UPDATE auth SET username='$username', password='$password', studycode='$studycode', description='$description', server='$server' WHERE `index` ='$id'");
Run Code Online (Sandbox Code Playgroud)

  • 您应该在这里清理输入以防止注入,无论是使用预处理语句还是转义值. (3认同)