www.webdeveloper.com
Results 1 to 15 of 15

Thread: Problem on inserting data into db

  1. #1
    Join Date
    May 2011
    Posts
    8

    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'strtotimemysql_real_escape_string($_POST['pcheckin'])));
    $date1 date('Y/m/d'strtotimemysql_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&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>
    PHP Code:

        <?php
    }
    ?>
    HTML Code:
    <input name="Submit" type="submit" value="Submit" /> 
    </form>
    So please help me out.

  2. #2
    Join Date
    Apr 2010
    Posts
    88
    Write die($query); before mysql_query() and post the result here.

  3. #3
    Join Date
    May 2011
    Posts
    8
    Quote Originally Posted by gvre View Post
    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')

  4. #4
    Join Date
    Apr 2010
    Posts
    88
    Could you post the table's schema?

  5. #5
    Join Date
    May 2011
    Posts
    8
    Quote Originally Posted by gvre View Post
    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

  6. #6
    Join Date
    Apr 2010
    Posts
    88
    '2011/05/22' is not a valid date. Use yyyymmdd or yyyy-mm-dd. Check mysql's manual for more details.

  7. #7
    Join Date
    May 2011
    Posts
    8
    Quote Originally Posted by gvre View Post
    '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.

  8. #8
    Join Date
    Apr 2010
    Posts
    88
    Could you post the output of SHOW CREATE TABLE packbook; ?

  9. #9
    Join Date
    May 2011
    Posts
    8
    Quote Originally Posted by gvre View Post
    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 ;

  10. #10
    Join Date
    Apr 2010
    Posts
    88
    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')"

  11. #11
    Join Date
    May 2011
    Posts
    8
    Quote Originally Posted by gvre View Post
    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...!

  12. #12
    Join Date
    Apr 2010
    Posts
    88
    It's working for me. Cound you write die($query); before mysql_query() and post the result here again?

  13. #13
    Join Date
    May 2011
    Posts
    8
    Quote Originally Posted by gvre View Post
    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')

  14. #14
    Join Date
    Apr 2010
    Posts
    88
    Use backticks ( ` ) instead of single quotes ( ' ).

  15. #15
    Join Date
    May 2011
    Posts
    8
    Quote Originally Posted by gvre View Post
    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
  •  
HTML5 Development Center



Recent Articles