homepage Welcome to WebmasterWorld Guest from 54.227.89.236
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Syntax error in query that I cant find - help save my brain!
lovduv




msg:3766385
 7:40 pm on Oct 15, 2008 (gmt 0)

Somwhere in the below block of code I have a missing, wrong, too many...quotes, back ticks, etc? It is throwing this error:

Couldn't execute query"insert into `mylists` (`name`,`owner`,`comments`,`completed`) values ('Me ','Me','testing''0', limit 1" error: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 'limit 1' at line 1

My versions are:
PHP version 4.4.7
MySQL version 4.1.22-standard

I added a record manually in phpMyAdmin, and my update and delete scripts work, just not the insert....If you can find the error...I'll give you a cookie! ; )


else{

$query = "insert into `mylists` (`name`,`owner`,`comments`,`completed`) values ";
$query .= "('".db_escape($_POST['name'])."','".db_escape($_POST['owner'])."','".db_escape($_POST['comments'])."'";

if($_POST['completed'] == 'yes'){
$query .= "'1', ";
}else{
$query .= "'0', ";
}

$query .= " limit 1";

if(mysql_query($query)){
$output = '<b>Item added successfully!</b><br/><br/>';
}else{
$output = mysql_query($query) or die("Couldn't execute query\"$query\" error:" . mysql_error());
}
}

 

LifeinAsia




msg:3766408
 8:13 pm on Oct 15, 2008 (gmt 0)

Look at your error message- there is no comma between 'testing' and '0' and you have no closing parenthesis.

henry0




msg:3766409
 8:16 pm on Oct 15, 2008 (gmt 0)

lovduv, welcome to WebmasterWorld!

I do not use that style (it's me)
instead I opt for:
$name=db_escape($_POST['name']);
$owner=db_escape($_POST['owner']);
$comments=db_escape($_POST['comments']);
$completed=db_escape($_POST['completed'']);

However you should before escaping verify that the expected POST values are the one expected...

$sql="insert into mylists (name, owner, comments, completed)
values
('$name', '$owner', '$comments', '$completed')";


lovduv




msg:3766468
 9:16 pm on Oct 15, 2008 (gmt 0)

henry0 -

Agreed, I usually opt for the above style as well, but I am adapting this script from another for my own needs....and so I am loathe to restructure the rest of the script : (

LifeinAsia - yeah I had messed with the code and got the comma in the right place, but missed the no closing parenthesis in that section....

I believe I have the comma correct now, but now I am unsure of where to put the closing parenthesis...below I added it to $query .= " limit 1"); and this is the error I get now:
Parse error: syntax error, unexpected ')' in ../list.php on line 214


else{

$query = "insert into `mylists` (`name`,`owner`,`comments`,`completed`) values ";
$query .= "('".db_escape($_POST['name'])."','".db_escape($_POST['owner'])."','".db_escape($_POST['comments'])."', ";

if($_POST['completed'] == 'yes'){
$query .= "'1' ";
}else{
$query .= "'0' ";
}

$query .= " limit 1");

if(mysql_query($query)){
$output = '<b>Item added successfully!</b><br/><br/>';
}else{
$output = mysql_query($query) or die("Couldn't execute query\"$query\" error:" . mysql_error());
}
}

lovduv




msg:3766587
 10:55 pm on Oct 15, 2008 (gmt 0)

Ok I got it with:

$query .= " 'limit 1')";

Thanks LifeinAsia for pointing me in the right direction : )

nick279




msg:3766920
 9:46 am on Oct 16, 2008 (gmt 0)

I used to use the INSERT INTO `tables` VALUES , but now tend to exclusvley use

INSERT INTO `table`
SET
`col1` = $col1,
`col2` = $col2 etc

Find it a bit easier to debug also

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved