Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am stumped with this syntax error (below):

    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 
    'desc, barkDesc, flowerDesc, foliageDesc, fruitDesc, type, cat, height, spread, l' 
    at line 1

What throws me off is the number 1 after spread like so (height, spread, 1).

Below is a php print of the INSERT INTO statement which looks great to me.

    INSERT INTO plants 
    (botanicalName, commonName, landscapeUses, desc, barkDesc, flowerDesc, foliageDesc, fruitDesc, type, cat, height, spread, lightReq, soil, growth, trimming, fert, otherMaint) 
    VALUES 
    ('Botanical name', 'Common Name', 'Accent, Edge, Groundcover', 'This is some text description text.', 'bark', 'flower', 'foliage', 'fruit', 'Perennial', '', '18 inches', '18 inches', 'full sun', 'well drained', 'medium', 'trimming', 'fertilization', 'other maintenance')

And without further ado, MY PHP code

       $sql2 = "INSERT INTO plants 
       (botanicalName, commonName, landscapeUses, desc, barkDesc, flowerDesc,  
       foliageDesc, fruitDesc, type, cat, height, spread, lightReq, soil, growth, 
       trimming, fert, otherMaint) 
       VALUES ('$botanicalName', '$commonName', '$usesString', '$desc', '$barkDesc',
       '$flowerDesc', '$foliageDesc', '$fruitDesc', '$type', '$cat', '$height', '$spread', '$lightReq', '$soil', '$growth', '$trimming', '$fert', '$otherMaint')";

       $result2 = mysql_query($sql2) or die(mysql_error());
       $plantID = mysql_insert_id();
       header("location: plantdetail.php?plantID=$plantID");
share|improve this question
add comment

closed as off-topic by Amal Murali, Chris, Second Rikudo, Sajeetharan, Barmar May 3 at 4:40

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Amal Murali, Chris, Second Rikudo, Sajeetharan, Barmar
If this question can be reworded to fit the rules in the help center, please edit the question.

4 Answers

up vote 3 down vote accepted

desc is a reserved keyword in mysql.

(use backticks to enclose desc)

 $sql2 = "INSERT INTO plants (botanicalName, commonName, landscapeUses,
 `desc`, barkDesc, flowerDesc, foliageDesc, fruitDesc, type, cat, 

--------^

  height, spread, lightReq, soil, growth, trimming, fert, otherMaint) 
             VALUES ()";
share|improve this answer
    
And? Complete your answer. –  sectus Jan 23 at 5:36
    
@sectus ya completed it,we need to enclose it within backticks –  R R Jan 23 at 5:38
1  
@Mr coder, you the man, that worked! I spent probably three hours on this analyzing the insert statement, checking for spellings, etc. I feel so dumb right now, haha. Will accept as soon as I can. –  Bill Chambers Jan 23 at 5:43
    
@BillChambers it happens with everybody :) enjoy coding :) –  R R Jan 23 at 5:43
1  
@sectus: Many times I feel that there should be a way to restrict commenting until someone completes minimum edit time required on a post, no matter whether it is a question or answer. –  Ravinder Jan 23 at 6:29
add comment

Since desc is a reserved keyword in MySQL, you need to quote it with backticks:

$sql2 = "INSERT INTO plants (botanicalName, commonName, landscapeUses, `desc`, barkDesc, flowerDesc, foliageDesc, fruitDesc, type, cat, height, spread, lightReq, soil, growth, trimming, fert, otherMaint) 
         VALUES ('$botanicalName', '$commonName', '$usesString', '$desc', '$barkDesc', '$flowerDesc', '$foliageDesc', '$fruitDesc', '$type', '$cat', '$height', '$spread', '$lightReq', '$soil', '$growth', '$trimming', '$fert', '$otherMaint')";
share|improve this answer
add comment

use this

 $sql =   "INSERT INTO plants 
(`botanicalName`, `commonName`, `landscapeUses`, `desc`, `barkDesc`, `flowerDesc`, `foliageDesc`, `fruitDesc`, `type`, `cat`, `height`, `spread`, `lightReq`, `soil`, `growth`, `trimming`, `fert`, `otherMaint`) 
VALUES 
('Botanical name', 'Common Name', 'Accent, Edge, Groundcover', 'This is some text description text.', 'bark', 'flower', 'foliage', 'fruit', 'Perennial', '', '18 inches', '18 inches', 'full sun', 'well drained', 'medium', 'trimming', 'fertilization', 'other maintenance')";
share|improve this answer
    
first of all you need to explain about the changes in your code,dont simply write code,include reasons also. :) –  R R Jan 23 at 5:52
add comment

As we know that desc is a keyword in mysql.

so when ever you are using keywords in table column then while using this keywords in query use of backticks to prevent error.

Try it in your case :

    $sql =   "INSERT INTO plants 
             (`botanicalName`, `commonName`, `landscapeUses`, `desc`, `barkDesc`, `flowerDesc`, `foliageDesc`, `fruitDesc`, `type`, `cat`, `height`, `spread`, `lightReq`, `soil`, `growth`, `trimming`, `fert`, `otherMaint`) 
             VALUES 
             ('Botanical name', 'Common Name', 'Accent, Edge, Groundcover', 'This is some text description text.', 'bark', 'flower', 'foliage', 'fruit', 'Perennial', '', '18 inches', '18 inches', 'full sun', 'well drained', 'medium', 'trimming', 'fertilization', 'other maintenance')";

for more information about the mysql KeyWords.

I hope it will help full to you.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.