Skip to main content
Participant
Dec 5, 2008 3:56 PM UTC
Answered

help with php mysql connect script

Ok guys im new to php mysql and i'm trying to insert some form information in my database. Im using a script from sitepoint in which i went through the tutorial did some experiments of my own and it worked....

but now i'm getting this error which i cant figure out....

Error placing order: 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 'order SET product ='1', size='medium', color='', quantity = '1' at line 1

here is the page
http://vaughntucker.com/imagecon/hats.php

and this is the script
<?php
//default page display
//connect to database
$dbcnx = @6688122_connect('p3nl41mysql7.secureserver.net', 'imagecon', 'Dub*boss_1');
if (!$dbcnx) {
echo '<p> Unable to connect to the '. 'database server at this time.</p>';
exit();

}

//select database
if (!@mysql_select_db('imagecon')) {
exit('<p>Unable to locate the ' .
'database at this time.</p>');
}

//Mysql query add joke
if (isset($_POST['submit'])) {
$product = 1;
$size = $_POST['size'];
$color = $_POST['color'];
$quan = $_POST['quantity'];
$sql = "INSERT INTO order SET
product ='$product',
size='$size',
color='$color',
quantity = '$quan',
date = CURDATE()";
}else{
echo 'No data submited';

}
if (@mysql_query($sql)) {
echo '<p>Your order has been submited.</p>';
} else {
echo '<p>Error placing order: ' .
mysql_error() . '</p>';
}



?>

thanks in advance
This topic is closed to new replies. Start a new post to keep the conversation going.
Newsgroup_UserCorrect answer
Inspiring
Dec 5, 2008 5:11 PM UTC
Murray *ACE* wrote:
> $sql = "INSERT INTO order SET
> product ='" . $product . "',
> size='" . $size . "',
> color='" . $color . "',
> quantity = '" . $quan . "',

No, that's not what's causing the problem. It's the use of "order" as
the table name. Order is a SQL reserved word. Either the table name
should be changed, or the INSERT query rewritten like this:

$size = mysql_real_escape_string($_POST['size']);
$color = mysql_real_escape_string($_POST['color']);
$quan = mysql_real_escape_string($_POST['quantity']);
$sql = "INSERT INTO `order` SET
product = 1,
size= '$size',
color= '$color',
quantity = $quan,
`date` = CURDATE()";

Note that I have surrounded order and date with backticks. I have also
removed $product, since it has a fixed value. I removed the quotes from
around $quan, because numbers should not be quoted in SQL queries.

The changes I have made assume that magic quotes are turned off on the
server. If they are on, you also need to pass the $_POST variables to
stripslashes() like this:

$size = mysql_real_escape_string(stripslashes($_POST['size']));
$color = mysql_real_escape_string(stripslashes($_POST['color']));
$quan = mysql_real_escape_string(stripslashes($_POST['quantity']);

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS4",
"PHP Solutions" & "PHP Object-Oriented Solutions"
http://foundationphp.com/

13 replies

Inspiring
Dec 5, 2008 5:18 PM UTC
<sigh>

Thanks, David! 8)

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


"David Powers" <david@example.com> wrote in message
news:ghbnbq$b1m$1@forums.macromedia.com...
> Murray *ACE* wrote:
>> $sql = "INSERT INTO order SET
>> product ='" . $product . "',
>> size='" . $size . "',
>> color='" . $color . "',
>> quantity = '" . $quan . "',
>
> No, that's not what's causing the problem. It's the use of "order" as the
> table name. Order is a SQL reserved word. Either the table name should be
> changed, or the INSERT query rewritten like this:
>
> $size = mysql_real_escape_string($_POST['size']);
> $color = mysql_real_escape_string($_POST['color']);
> $quan = mysql_real_escape_string($_POST['quantity']);
> $sql = "INSERT INTO `order` SET
> product = 1,
> size= '$size',
> color= '$color',
> quantity = $quan,
> `date` = CURDATE()";
> Note that I have surrounded order and date with backticks. I have also
> removed $product, since it has a fixed value. I removed the quotes from
> around $quan, because numbers should not be quoted in SQL queries.
>
> The changes I have made assume that magic quotes are turned off on the
> server. If they are on, you also need to pass the $_POST variables to
> stripslashes() like this:
>
> $size = mysql_real_escape_string(stripslashes($_POST['size']));
> $color = mysql_real_escape_string(stripslashes($_POST['color']));
> $quan = mysql_real_escape_string(stripslashes($_POST['quantity']);
>
> --
> David Powers, Adobe Community Expert
> Author, "The Essential Guide to Dreamweaver CS4",
> "PHP Solutions" & "PHP Object-Oriented Solutions"
> http://foundationphp.com/

Translate
Newsgroup_UserCorrect answer
Inspiring
Dec 5, 2008 5:11 PM UTC
Murray *ACE* wrote:
> $sql = "INSERT INTO order SET
> product ='" . $product . "',
> size='" . $size . "',
> color='" . $color . "',
> quantity = '" . $quan . "',

No, that's not what's causing the problem. It's the use of "order" as
the table name. Order is a SQL reserved word. Either the table name
should be changed, or the INSERT query rewritten like this:

$size = mysql_real_escape_string($_POST['size']);
$color = mysql_real_escape_string($_POST['color']);
$quan = mysql_real_escape_string($_POST['quantity']);
$sql = "INSERT INTO `order` SET
product = 1,
size= '$size',
color= '$color',
quantity = $quan,
`date` = CURDATE()";

Note that I have surrounded order and date with backticks. I have also
removed $product, since it has a fixed value. I removed the quotes from
around $quan, because numbers should not be quoted in SQL queries.

The changes I have made assume that magic quotes are turned off on the
server. If they are on, you also need to pass the $_POST variables to
stripslashes() like this:

$size = mysql_real_escape_string(stripslashes($_POST['size']));
$color = mysql_real_escape_string(stripslashes($_POST['color']));
$quan = mysql_real_escape_string(stripslashes($_POST['quantity']);

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS4",
"PHP Solutions" & "PHP Object-Oriented Solutions"
http://foundationphp.com/
Translate
Inspiring
Dec 5, 2008 4:00 PM UTC
$sql = "INSERT INTO order SET
product ='" . $product . "',
size='" . $size . "',
color='" . $color . "',
quantity = '" . $quan . "',


I believe there are other more elegant ways to do this, as well.

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


"beatzMalone" <webforumsuser@macromedia.com> wrote in message
news:ghbiup$5s3$1@forums.macromedia.com...
> Ok guys im new to php mysql and i'm trying to insert some form information
> in
> my database. Im using a script from sitepoint in which i went through the
> tutorial did some experiments of my own and it worked....
>
> but now i'm getting this error which i cant figure out....
>
> Error placing order: 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
> 'order SET product ='1', size='medium', color='', quantity = '1' at line 1
>
> here is the page
> http://vaughntucker.com/imagecon/hats.php
>
> and this is the script
> <?php
> //default page display
> //connect to database
> $dbcnx = @mysql_connect('p3nl41mysql7.secureserver.net', 'imagecon',
> 'Dub*boss_1');
> if (!$dbcnx) {
> echo '<p> Unable to connect to the '. 'database server at this
> time.</p>';
> exit();
>
> }
>
> //select database
> if (!@mysql_select_db('imagecon')) {
> exit('<p>Unable to locate the ' .
> 'database at this time.</p>');
> }
>
> //Mysql query add joke
> if (isset($_POST['submit'])) {
> $product = 1;
> $size = $_POST['size'];
> $color = $_POST['color'];
> $quan = $_POST['quantity'];
> $sql = "INSERT INTO order SET
> product ='$product',
> size='$size',
> color='$color',
> quantity = '$quan',
> date = CURDATE()";
> }else{
> echo 'No data submited';
>
> }
> if (@mysql_query($sql)) {
> echo '<p>Your order has been submited.</p>';
> } else {
> echo '<p>Error placing order: ' .
> mysql_error() . '</p>';
> }
>
>
>
> ?>
>
> thanks in advance
>

Translate