mysql更新的代码,PHP / MySQL更新代码

在尝试使用PHP更新MySQL数据库中的一行记录时遇到问题,而插入新行的操作却能正常工作。错误出现在UPDATE语句中,可能是因为'index'是保留关键字导致的。在MySQL管理工具中直接执行相同的SQL命令也返回了语法错误。解决方案是将'index'用反引号包裹以避免语法冲突。
摘要由CSDN通过智能技术生成

I'm trying to call a row update from php to an mysql database. It fails out but when i try to call an insert new row formated just the same it works.

$result = mysql_query("INSERT INTO auth (username, password, studycode, description, server) VALUES ('$username', '$password', '$studycode', '$description', '$server')");

but this code fails

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

index is the first column and its the key/id for the table.

Edit:

Ok so i just went into mysql admin and tried the exact command my code would have sent to track the error.

UPDATE auth SET username='username', password='password', studycode='ab9102y', description='test change', server='server2' WHERE index='5' LIMIT 1;

gives me the error

#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

解决方案

Possibly a reserved keyword issue with index. Try:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值