我有 Mysql 查询错误

I have an error With Mysql Query

$sql="update item_master set department=$dept,make=$make,vat=$vat,cost=$mrp,packing=$pack,unit=$unit,exp_date=$ex,stock=$stock,description=$desc
where item_code=$id";
mysql_select_db('pds', $conn);

$result = mysql_query($sql);

错误是:

-id= 1dept=Ayurvedicmake=arihantname=sample2vat=4mrp=100pack=1*100Could not run query: 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 'where item_code= 1' at line 2

这里有一些错误。

看到你的很多变量都是字符串,需要用引号引起来。

我发现的另一件事是 description=$descwhere 之间没有 space。

例如:如果 $desc 定义为 testing,则 MySQL 将其解释为:

description=testingwhere item_code=$id

这将出于几个原因解释该错误,一个未引用的变量和缺少的 space。

将您的代码更改为以下内容。

$sql="UPDATE item_master SET 
department = '$dept', make = '$make', vat = '$vat', cost = '$mrp', 
packing = $pack, unit = '$unit', exp_date= '$ex', stock = '$stock', 
description = '$desc' WHERE item_code=$id";

旁注:函数和子句最好使用大写字母。它有助于将它们与您的其余代码区分开来。

此外,您的代码很容易 SQL 注入。考虑使用准备好的语句,或者暂时清理您的代码,直到您这样做。

另一个重要说明。

如果您仍然遇到问题,可能是插入了 MySQL 抱怨的字符。因此,您需要对数据进行转义。

举个例子,剩下的为其他人做:

mysql_real_escape_string($dept);
  • 仔细检查您的列类型,使其与数据及其长度匹配。

参考文献:

更改您的查询

$id需要点赞'$id'

$sql="update item_master set department=$dept,make=$make,
vat=$vat,cost=$mrp,packing=$pack,unit=$unit,exp_date=$ex,
stock=$stock,description=$desc where item_code='$id'";
mysql_select_db('pds', $conn);

$result = mysql_query($sql);