简体   繁体   中英

MySQL INSERT Syntax error and unsure why

im getting the following error with my query from 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

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

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

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 . 指定所有列的值。

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.

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.

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

$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

Please note that you don't need to append that

; in between your mysql query

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM