简体   繁体   中英

whats wrong with this query

when i insert the record to mysql it give me error

$sql = "insert into fish (fish_id,common_name,scientific_name,family,range,habitate,adult_size,identification,how_to_fish,image) values ('$com_name','$scientific_name','$family','$range','$habitate','$adult_size','$identification','$how_to_fish','$TARGET_PATH')";

the error is

Could not insert data into DB: 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 ' range,habitate,adult_size,identification,how_to_fish,image) values ('Suwannee Ba ' at line 1

when i dump the query it shows that all the fields are correct.

string(737) "insert into fish (catch_id,common_name,scientific_name,family,range,habitate,adult_size,identification,
how_to_fish,image) values ('','Suwannee Bass','Micropterus notius','Centrarchidae (Sunfish)','United States (Florida, Georgia)',
'Freshwater: found in Suwannee and Ochlockonee river drainages of Florida and Georgia.',
'Up to 12 oz (.34 kg).','The smallest of the Black Bass; brown with dark markings along back and sides. Adult male has blue cheeks, breast, and belly.',
'Natural or artificial bait such as spinners, spoons, crankbaits, surface plugs, and plastic worms. Also can be caught via fly fishing using bugs, streamers, and bucktails. Live bait includes worms, crayfish, leeches, and minnows.',
'localhost/fish/pics/Smallmouth bass.png')"

note that my (catch_id) is auto increament and i checked this without inserting catch_id but the same problem is there

RANGE is a MySQL reserved word, as such you need to wrap your range field name in backticks, otherwise MySQL will get confused.

insert into fish (fish_id,common_name,scientific_name,family,`range`,habitate...

Then again you should probably just enclose your table name and all your field names in backticks. Or, rename the range field so you don't have to use backticks.

RANGE is a MySQL keyword. You should put all field and table names in quotes:

`range`

Your query should start with

insert into fish (common_name,scientific_name

Note that id field is not listed here.

If your catch_id is not nullable, you should specify its value int the query too:

insert into fish (catch_id,common_name,... ) values (1/*catch_id*/,'$com_name',...)

您指定10个字段,但仅提供9个

either u should change the table field name or put into `` and
Better to use another syntax of inserting a single row at a time

$sql="INSERT INTO fish SET
               common_name='$com_name',
               scientific_name='$scientific_name',
               family='$family',
               `range`='$range',
               habitate='$habitate',
               adult_size='$adult_size',
               identification='$identification',
               how_to_fish='$how_to_fish',
               image='$TARGET_PATH' ";

i think this will lead to less chance of error and if it occurred then easy to identify.

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