#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    13
    Rep Power
    0

    Exclamation Error while inserting into MySQL Database


    I have a table in my webpage that comes from MySql database. And on the same table, I have one of row where I have build checkbox to insert choose data from the same table to insert. I am not getting any data out of it. I might have error on PHP to insert.
    This is the link if you want to see my page.. http://homer.wcitac.org/~kkhat90257/lumbini/onlineOrder.php

    This is my Code
    PHP Code:
    <?php
    session_start 
    ();

        
    /*******************************
        |||||Setting Path of Files||||||
        *******************************/
    $path "../includes";
    set_include_path(get_include_path() . PATH_SEPARATOR $path); 
    ob_start();
    require_once(
    'manager.inc');

        
    /*******************************
        ||||||||Get the connection||||||
        *******************************/
        
    $conn DBManager::getConnection();
        
    //set up character set
        
    $conn ->query("SET NAMES 'utf8' ");
                   
        
    /*********************************
        ||||||||Displaying Table||||||||||
        **********************************/
      
    $appetizerNo ""
    if(isset(
    $_POST['appetizerNo'])){ $notify $_POST['appetizerNo']; }
    //checking first whether the check box is CHECKED (or set) using a condition. And if the condition is true I am getting the value passed.
    //Reference: http://notesofgenius.com/how-fix-php-notice-undefined-index/
                                      
    //$appetizerNo = $_POST['appetizerNo'];   Will get “Warning: Undefined index:”
        
    if(isset($_POST['FoodLineItems']))
        {
          foreach(
    $_POST['FoodLineItems'] as $appetizerNo){
            
    $_SESSION['appetizerNo'] = $appetizerNo;
        }
        }
        
        
    $query_str"SELECT appetizerNo, name, description, price FROM FoodAppetizers";
        (
    $result $conn->query($query_str));
        echo 
    "<p><b>Lists of appitizer foods:</b></p>"
       
        if (
    $result === FALSE)
      {
        
    $errno $conn->errno;
        
    $errmsg $conn->error;
        echo 
    "Connect Failed with: ($errno$errmsg<br/>\n";
        
    $conn->close();
        exit;
      }
      else
      {
          echo <<<EOM
        <form method="post" action="">
        <table border='1'>
        <tr>
          <td>Appetizer No</td>
          <td>Name</td>
          <td>Description</td>
          <td>Price</td>
          <td>Cart</td>
        </tr>
    EOM;

        while ((
    $row $result->fetch_assoc()) !== NULL)
        {
          echo <<<EOM
      <tr>
        <td>
    {$row["appetizerNo"]}</td>
        <td>
    {$row["name"]}</td>
        <td>
    {$row["description"]}</td>
        <td>
    {$row["price"]}</td>
        <td><input type="checkbox" name="FoodLineItems[]" value=".
    $appetizerNo."/></td>
      </tr>

    EOM;
        }

        echo <<<EOTABLE

      </table>
      <input type="submit" name="submit" value="submit">
      </form>

    EOTABLE;

    //Passing "Add to Cart" into database: http://www.daniweb.com/web-development/php/threads/108189/inserting-checkbox-values-in-mysql-php
     
    if(isset($_POST['FoodLineItems']))
    {
        foreach(
    $_POST['FoodLineItems'] as $appetizerNo){
          
    //$insert=mysql_query("INSERT INTO FoodLineItems('appetizerNo') VALUES ('.$appetizerNo.')");
          
    $query_str="INSERT INTO FoodLineItems('appetizerNo') VALUES ('.$appetizerNo.')";
          echo 
    "<br/>";
          echo 
    $query_str "<br/>";
          if ( 
    $conn->query($query_str)) {
              
    $appetizerNo $conn->insert_id;
              
    //echo "Your order was sent to OrderLineItem Table. <br/>";
          
    }
          else {
              echo 
    "Table creation failed: (" $conn->errno ") " $conn->error;
          }
           
    //$result = $conn->query($query_str);   
           //echo $result->
      
          
    }
    }

        
    //
        // clean up resultsets when we're done with them!
        //
        
    $result->close();
      }
      
     
    /* //THis is try to get data from Form without passing to database (NOT WORKING)
    if (isset($_POST['submit'])){
      foreach($_POST['FoodLineItems'] as $appetizerNo){
          echo ". $appetizerNo .";
      }
    }
    */
    ob_end_flush();
    session_destroy();

    ?>
    This is the error i am getting:
    NSERT INTO FoodLineItems('appetizerNo') VALUES ('............')
    Table creation failed: (1064) 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 ''appetizerNo') VALUES ('............')' at line 1
    INSERT INTO FoodLineItems('appetizerNo') VALUES ('............')
    Table creation failed: (1064) 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 ''appetizerNo') VALUES ('............')' at line 1

    As you can see. It is passing '.....' value.

    I will greatly appreciate your help!!
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,007
    Rep Power
    9398
    PHP Code:
    $query_str="INSERT INTO FoodLineItems('appetizerNo') VALUES ('.$appetizerNo.')"
    In MySQL, double " and single ' quotes are for strings only while backticks ` are for names.

    To answer your next question, pick one method of getting a variable into a string and stick with it - right now you're trying two different methods at the same time (and not getting either one quite right).
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    18
    Check the value of appetizerNo in your database, it looks like '....' is saved in all the columns.

    In the querystring:
    $query_str="INSERT INTO FoodLineItems('appetizerNo') VALUES ('.$appetizerNo.')";

    I think you mean $row["appetizerNo"], rather than $appetizerNo

    Comments on this post

    • ManiacDan disagrees : (0) please read the whole thread before contributing. This answer is incorrect
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Location
    Manchester
    Posts
    10
    Rep Power
    0
    Don't surround your column names with quotes....

    PHP Code:
    $query "INSERT INTO table_name (column1,column2...) VALUES ('.$value1.', '.$value2.')"
    Hope that helps

IMN logo majestic logo threadwatch logo seochat tools logo