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 :
PHP Code:
<?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
{
?>
And my form is like :
HTML Code:
<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 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 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 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 Select</option>
<option value="" > Budget </option>
<option value="" > Luxury</option>
<option value="" > Semi-Luxury</option>
<option value="" > 5 Star</option>
<option value="" > 3 Star</option>
</select>
<select name="trans" size="1" >
<option> Please Select</option>
<option> Normal Car</option>
<option> AC Car</option>
<option> SUV</option>
<option> Tourister</option>
<option> Mini Bus</option>
</select>
HTML Code:
<input name="Submit" type="submit" value="Submit" />
</form>
So please help me out.
Write die($query); before mysql_query() and post the result here.
Originally Posted by
gvre
Write die($query); before mysql_query() and post the result here.
INSERT INTO packbook( name, add, add1, mobile, email, checkin, checkout, package, adult, children, hotel, transport) VALUES( 'naren', 'sen niwas', 'sandal', '3456789', 'nandy.sen9@gmail.com', '2011/05/22', '2011/05/27', 'Delhi-Shimla-Delhi', '2', '2', '', 'AC Car')
Could you post the table's schema?
Originally Posted by
gvre
Could you post the table's schema?
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
'2011/05/22' is not a valid date. Use yyyymmdd or yyyy-mm-dd. Check mysql's manual for more details.
Originally Posted by
gvre
'2011/05/22' is not a valid date. Use yyyymmdd or yyyy-mm-dd. Check
mysql's manual for more details.
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.
Could you post the output of SHOW CREATE TABLE packbook; ?
Originally Posted by
gvre
Could you post the output of SHOW CREATE TABLE packbook; ?
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=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
add is a reserved word for mysql. You need to enclose the field name inside backticks. Try the following
PHP Code:
$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 ')" ;
Originally Posted by
gvre
add is a reserved word for mysql. You need to enclose the field name inside backticks. Try the following
PHP Code:
$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 ')" ;
sry but error is still same...!
It's working for me. Cound you write die($query); before mysql_query() and post the result here again?
Originally Posted by
gvre
It's working for me. Cound you write die($query); before mysql_query() and post the result here again?
INSERT INTO packbook( name, 'add', add1, mobile, email, checkin, checkout, package, adult, children, hotel, transport) VALUES('naren', 'sen niwas', 'sandal', '3456789', 'nandy.sen9@gmail.com', '2011-05-22', '2011-05-27', 'Delhi-Shimla-Delhi', '1', '3', '', 'Tourister')
Use backticks ( ` ) instead of single quotes ( ' ).
Originally Posted by
gvre
Use backticks ( ` ) instead of single quotes ( ' ).
Thank you sir now its working.......! you are great....!!!! Thanks again...!
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks