我有一个Mysql查询错误

时间:2015-06-14 22:40:13

标签: php mysql

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {

CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                     recognizer.view.center.y + translation.y);
//[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
UIButton *button = (UIButton*)recognizer.view;
if (recognizer.state == UIGestureRecognizerStateEnded)
{

    button.center = recognizer.view.center;
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

}

}

错误是:

  

-id = 1dept = Ayurvedicmake = arihantname = sample2vat = 4mrp = 100pack = 1 * 100无法运行查询:SQL语法中有错误;检查与您的MySQL服务器版本对应的手册,以便在' where item_code = 1'附近使用正确的语法。在第2行

2 个答案:

答案 0 :(得分:0)

这里有一些问题。

看到许多变量都是字符串,需要引用它们。

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

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

description=testingwhere item_code=$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";

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

另外,您的代码非常容易注入SQL。考虑使用预准备语句,或者现在清理代码,直到你这样做。

另一个重要的注意事项。

如果您仍然遇到问题,那么可能会插入MySQL可能会抱怨的字符。因此,您需要转义数据。

示例,并为其他人做其余的事情:

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

参考文献:

答案 1 :(得分:0)

更改您的查询

$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);