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

时间:2021-09-16 02:05:44

im getting the following error with my query from PHP:

我从PHP的查询得到以下错误:

Does anyone have any ideas on why? its passing an email address and from error looks like its breaking there but i might be wrong.

有没有人对为什么有任何想法?它通过一个电子邮件地址和错误看起来像它打破那里但我可能是错的。

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 'INSERT INTO customers SET name = "James Brandon", email = "james@ambient' at line 10

您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以便在'INSERT INTO customers SET name =“James Brandon”附近使用正确的语法,在第10行发送电子邮件=“james @ ambient”

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 个解决方案

#1


Two possible syntax for INSERT statement. In the first following case, you specify only col you want to fill.

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

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

You can also INSERT without providing col_name but you will have to specify value of all columns and in the good order.

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

The first opton is better in my opinion and will avoid you many mistakes especially when you have a lot of different column in your table.

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

#2


According to this reference, you should try something like this:

根据这个参考,你应该尝试这样的事情:

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

Edit: As mentioned in the comments to your answer, please consider using PDO. It is much safer and in my opinion even easier to handle.

编辑:如您对答案的评论中所述,请考虑使用PDO。它更安全,在我看来更容易处理。

#3


@James Since you are doing INSERT command. Do as the following.

@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."')";

Thanks & Regards,

感谢和问候,

Vivek

#4


Please note that you don't need to append that

请注意,您无需附加该内容

; in between your mysql query

;在你的mysql查询之间

your modified query will be

您修改后的查询将是

 $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."'
            ";

Please note there is a

请注意有一个

semi-colon after $customer_postcode_ship and no need of the . in $query

$ customer_postcode_ship之后的分号并不需要。在$ query中

#1


Two possible syntax for INSERT statement. In the first following case, you specify only col you want to fill.

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

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

You can also INSERT without providing col_name but you will have to specify value of all columns and in the good order.

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

The first opton is better in my opinion and will avoid you many mistakes especially when you have a lot of different column in your table.

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

#2


According to this reference, you should try something like this:

根据这个参考,你应该尝试这样的事情:

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

Edit: As mentioned in the comments to your answer, please consider using PDO. It is much safer and in my opinion even easier to handle.

编辑:如您对答案的评论中所述,请考虑使用PDO。它更安全,在我看来更容易处理。

#3


@James Since you are doing INSERT command. Do as the following.

@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."')";

Thanks & Regards,

感谢和问候,

Vivek

#4


Please note that you don't need to append that

请注意,您无需附加该内容

; in between your mysql query

;在你的mysql查询之间

your modified query will be

您修改后的查询将是

 $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."'
            ";

Please note there is a

请注意有一个

semi-colon after $customer_postcode_ship and no need of the . in $query

$ customer_postcode_ship之后的分号并不需要。在$ query中