/    Sign up×
Community /Pin to ProfileBookmark

Problem on inserting data into db

Hello every body I am using a form in which i have 12 fields. But when i click on submit button then i got error :
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 ‘add, add1, mobile, email, checkin, checkout, package, adult, children, hotel, tr’ at line 1

For more information my php code is below :

[code=php]
<?php

$host=”localhost”; // Host name
$username=”root”; // Mysql username
$password=”*******”; // Mysql password
$db_name=”book”; // Database name
$tbl_name=”packbook”; // Table name

// Connect to server and select databse.
$connect = mysql_connect(“$host”, “$username”, “$password”)or die(“cannot connect”);
mysql_select_db(“$db_name”)or die(“cannot select DB”);
?>
<?php

if (isset($_REQUEST[‘Submit’]))
{

$pname = mysql_real_escape_string($_POST[‘pname’]);
$padd = mysql_real_escape_string($_POST[‘padd’]);
$padd1 = mysql_real_escape_string($_POST[‘padd1’]);
$pmob = mysql_real_escape_string($_POST[‘pmobile’]);
$pmail = mysql_real_escape_string($_POST[‘pemail’]);
$date = date(‘Y/m/d’, strtotime( mysql_real_escape_string($_POST[‘pcheckin’])));
$date1 = date(‘Y/m/d’, strtotime( mysql_real_escape_string ($_POST[‘pcheckout’])));
$ppack = mysql_real_escape_string($_POST[‘pack’]);
$padult = mysql_real_escape_string($_POST[‘adult’]);
$pchild = mysql_real_escape_string($_POST[‘child’]);
$photel = mysql_real_escape_string($_POST[‘hotel’]);
$ptrans = mysql_real_escape_string($_POST[‘trans’]);
$query = “INSERT INTO packbook( name, add, add1, mobile, email, checkin, checkout, package, adult, children, hotel, transport) VALUES( ‘$pname’, ‘$padd’, ‘$padd1’, ‘$pmob’, ‘$pmail’, ‘$date’, ‘$date1’, ‘$ppack’, ‘$padult’, ‘$pchild’, ‘$photel’, ‘$ptrans’)”;
if($result = mysql_query($query ,$connect))
{
echo “Thank you, Your information has been entered into our database”;
}
else
{
echo “ERROR: “.mysql_error();
}

}
else
{
?>

[/code]

And my form is like :

[code=html]
<form action=”” method=”post” >
<input type=”text” name=”pname” size=”27″ style=”width:192px;” value=””>
<input type=”text” name=”padd” size=”27″ style=”width:192px;” value=””>
<input type=”text” name=”padd1″ size=”27″ style=”width:192px;” value=””>
<input type=”text” name=”pmobile” size=”27″ style=”width:192px;” value=””>
<select name=”pack” size=”1″>
<option>Please&nbsp;Select</option>
<option>Delhi-Shimla-Delhi</option>
</select>
<input type=”text” name=”pemail” size=”27″ style=”width:192px;” value=””>
<input type=”text” name=”pcheckin” size=”27″ style=”width:192px;” value=””>
<input type=”text” name=”pcheckout” size=”27″ style=”width:192px;” value=””>
<select name=”adult” size=”1″>
<option>Please&nbsp;Select</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
</select>
<select name=”child” size=”1″>
<option>Please&nbsp;Select</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
</select>
<select name=”hotel” size=”1″>
<option value=””>Please&nbsp;Select</option>
<option value=””>Budget&nbsp;</option>
<option value=””>Luxury</option>
<option value=””>Semi-Luxury</option>
<option value=””>5&nbsp;Star</option>
<option value=””>3&nbsp;Star</option>
</select>
<select name=”trans” size=”1″>
<option>Please&nbsp;Select</option>
<option>Normal&nbsp;Car</option>
<option>AC&nbsp;Car</option>
<option>SUV</option>
<option>Tourister</option>
<option>Mini&nbsp;Bus</option>
</select>
[/code]

[code=php]

<?php
}
?>
[/code]

[code=html]

<input name=”Submit” type=”submit” value=”Submit” />
</form>
[/code]

So please help me out.

to post a comment
PHP

14 Comments(s)

Copy linkTweet thisAlerts:
@gvreMay 19.2011 — Write die($query); before mysql_query() and post the result here.
Copy linkTweet thisAlerts:
@narensenauthorMay 19.2011 — Write die($query); before mysql_query() and post the result here.[/QUOTE]
INSERT INTO packbook( name, add, add1, mobile, email, checkin, checkout, package, adult, children, hotel, transport) VALUES( 'naren', 'sen niwas', 'sandal', '3456789', '[email protected]', '2011/05/22', '2011/05/27', 'Delhi-Shimla-Delhi', '2', '2', '', 'AC Car')
Copy linkTweet thisAlerts:
@gvreMay 19.2011 — Could you post the table's schema?
Copy linkTweet thisAlerts:
@narensenauthorMay 19.2011 — Could you post the table's schema?[/QUOTE]
id int(50) No None auto_increment


name varchar(50) latin1_swedish_ci Yes NULL


add varchar(50) latin1_swedish_ci Yes NULL


add1 varchar(50) latin1_swedish_ci Yes NULL


mobile varchar(50) latin1_swedish_ci Yes NULL


email varchar(50) latin1_swedish_ci Yes NULL


checkin date Yes NULL


checkout date Yes NULL


package varchar(50) latin1_swedish_ci Yes NULL


adult varchar(50) latin1_swedish_ci Yes NULL


children varchar(50) latin1_swedish_ci Yes NULL


hotel varchar(50) latin1_swedish_ci Yes NULL


transport varchar(50) latin1_swedish_ci Yes NULL
Copy linkTweet thisAlerts:
@gvreMay 19.2011 — '2011/05/22' is not a valid date. Use yyyymmdd or yyyy-mm-dd. Check mysql's manual for more details.
Copy linkTweet thisAlerts:
@narensenauthorMay 19.2011 — '2011/05/22' is not a valid date. Use yyyymmdd or yyyy-mm-dd. Check mysql's manual for more details.[/QUOTE]
ya i did the same but error is still same...! please will u see in error statement before 'AC Car' there is inverted comma what's that...but in my query there is no comma at that place.
Copy linkTweet thisAlerts:
@gvreMay 19.2011 — Could you post the output of SHOW CREATE TABLE packbook; ?
Copy linkTweet thisAlerts:
@narensenauthorMay 19.2011 — Could you post the output of SHOW CREATE TABLE packbook; ?[/QUOTE]

Table structure for table packbook
--



CREATE TABLE IF NOT EXISTS packbook (

id int(50) NOT NULL AUTO_INCREMENT,

name varchar(50) DEFAULT NULL,

add varchar(50) DEFAULT NULL,

add1 varchar(50) DEFAULT NULL,

mobile varchar(50) DEFAULT NULL,

email varchar(50) DEFAULT NULL,

checkin date DEFAULT NULL,

checkout date DEFAULT NULL,

package varchar(50) DEFAULT NULL,

adult varchar(50) DEFAULT NULL,

children varchar(50) DEFAULT NULL,

hotel varchar(50) DEFAULT NULL,

transport varchar(50) DEFAULT NULL,

PRIMARY KEY (id)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Copy linkTweet thisAlerts:
@gvreMay 19.2011 — add is a reserved word for mysql. You need to enclose the field name inside backticks. Try the following
[code=php]$query = "INSERT INTO packbook( name, add, add1, mobile, email, checkin, checkout, package, adult, children, hotel, transport) VALUES( '$pname', '$padd', '$padd1', '$pmob', '$pmail', '$date', '$date1', '$ppack', '$padult', '$pchild', '$photel', '$ptrans')"; [/code]
Copy linkTweet thisAlerts:
@narensenauthorMay 19.2011 — add is a reserved word for mysql. You need to enclose the field name inside backticks. Try the following
[code=php]$query = "INSERT INTO packbook( name, add, add1, mobile, email, checkin, checkout, package, adult, children, hotel, transport) VALUES( '$pname', '$padd', '$padd1', '$pmob', '$pmail', '$date', '$date1', '$ppack', '$padult', '$pchild', '$photel', '$ptrans')"; [/code][/QUOTE]

sry but error is still same...!
Copy linkTweet thisAlerts:
@gvreMay 19.2011 — It's working for me. Cound you write die($query); before mysql_query() and post the result here again?
Copy linkTweet thisAlerts:
@narensenauthorMay 19.2011 — It's working for me. Cound you write die($query); before mysql_query() and post the result here again?[/QUOTE]
INSERT INTO packbook( name, 'add', add1, mobile, email, checkin, checkout, package, adult, children, hotel, transport) VALUES('naren', 'sen niwas', 'sandal', '3456789', '[email protected]', '2011-05-22', '2011-05-27', 'Delhi-Shimla-Delhi', '1', '3', '', 'Tourister')
Copy linkTweet thisAlerts:
@gvreMay 19.2011 — Use backticks ( ` ) instead of single quotes ( ' ).
Copy linkTweet thisAlerts:
@narensenauthorMay 20.2011 — Use backticks ( ` ) instead of single quotes ( ' ).[/QUOTE]
Thank you sir now its working.......! you are great....!!!! Thanks again...!
×

Success!

Help @narensen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...