MySQL INSERT语法错误并且不确定原因

时间:2015-05-07 07:45:18

标签: php mysql mysqli

我的PHP查询出现以下错误:

有没有人对为什么有任何想法?它传递了一个电子邮件地址,并且从错误看起来就像它在那里打破,但我可能错了。

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近' INSERT INTO客户SET                 name =" James Brandon",                 email =" james @ ambient'在第10行

PHP

$query .= "INSERT INTO customers SET
                name = '".$customer_name."',
                email = '".$customer_email."',
                address_1 = '".$customer_address_1."',
                address_2 = '".$customer_address_2."',
                town = '".$customer_town."',
                county = '".$customer_county."',
                postcode = '".$customer_postcode."',
                phone = '".$customer_phone."',

                name_ship = '".$customer_name_ship."',
                address_1_ship = '".$customer_address_1_ship."',
                address_2_ship = '".$customer_address_2_ship."',
                town_ship = '".$customer_town_ship."',
                county_ship = '".$customer_county_ship."',
                postcode_ship = '".$customer_postcode_ship."';
            ";

4 个答案:

答案 0 :(得分:2)

INSERT语句的两种可能语法。 在下面的第一种情况中,您只需指定要填充的col。

INSERT INTO TABLE (COL1, COL2, COL3)
VALUES (VAL_COL1, VAL_COL2, VAL_COL3);

您也可以在不提供col_name的情况下进行INSERT,但是您必须指定所有列的值并且正确的顺序

在我看来,第一个opton更好,并且会避免很多错误,特别是当你的表中有很多不同的列时。

答案 1 :(得分:1)

根据this reference,你应该尝试这样的事情:

INSERT INTO customers (name , email, address_1, ...)
VALUES (value1, value2, value3,...)

修改:正如您的回答评论中所述,请consider using PDO。它更安全,在我看来更容易处理。

答案 2 :(得分:0)

@James因为你正在做INSERT命令。请执行以下操作。

$query = "INSERT INTO customers (name, email, address_1, address_2, town, county, postcode, phone, name_ship, address_1_ship, address_2_ship, town_ship, county_ship, postcode_ship) VALUES('".$customer_name."', '".$customer_email."', '".$customer_address_1."', '".$customer_address_2."', '".$customer_town."', '".$customer_county."', '".$customer_postcode."', '".$customer_phone."', '".$customer_name_ship."', '".$customer_address_1_ship."', '".$customer_address_2_ship."', '".$customer_town_ship."', '".$customer_county_ship."', '".$customer_postcode_ship."')";

谢谢&的问候,

<强>的Vivek

答案 3 :(得分:0)

请注意,您不需要附加

  

在你的mysql查询之间

您的修改后的查询将是

 $query =      "INSERT INTO customers SET name = '".$customer_name."',

                  email = '".$customer_email."',

                  address_1 = '".$customer_address_1."',

                  address_2 = '".$customer_address_2."',

                  town = '".$customer_town."',

                  county = '".$customer_county."',

                  postcode = '".$customer_postcode."',

                  phone = '".$customer_phone."',

                  name_ship = '".$customer_name_ship."',

                  address_1_ship = '".$customer_address_1_ship."',

                  address_2_ship = '".$customer_address_2_ship."',

                  town_ship = '".$customer_town_ship."',

                  county_ship = '".$customer_county_ship."',

                  postcode_ship = '".$customer_postcode_ship."'
            ";

请注意有一个

  $customer_postcode_ship之后的

分号,.

中不需要$query