Click here to Skip to main content
15,179,684 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to insert to order table from cart table .but i have faced this problem
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'order(cid,pid,pname,price) VALUES('1','6','Refrigerator','30000.00')' at line 141.

What I have tried:

<pre>My code are
<pre> public function Order($id)
  {
    $sid= session_id();
     $squery = "SELECT * FROM cart WHERE sid = '$sid'";
      $result= $this->db->select($squery);
      if ($result) {
        while ($value = $result->fetch_assoc()) {
          $pid      = $value['pid'];
          $pname    = $value['pname'];
          $price    = $value['price'];
          $quantity = $value['quantity'];
          $image    = $value['image'];
         $query = "INSERT INTO order(cid,pid,pname,price,quantity,image) VALUES('$id','$pid','$pname','$price','$quantity',$image)";
          $insert_product = $this->db->insert($query);
        
        }
      }
  }
 }
Posted
Updated 6-Apr-21 12:13pm

Two things:
1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

2) Don't call columns the same name as SQL keywords: ORDER is a keyword (part of the ORDER BY command) so when SQL meets it in your command it can't work out what you mean. It is possible to use keywords as columns or table names, but you have to escape them by surrounding them with square brackets, just as you would for a name containing a space character.
   
The error message does not match the SQL (there are 4 col names in the error message and 6 in the SQL statement) so the 'what I have tried' is not what was actually tried.

I am guessing that cid, pid, price and quantity are numeric but you are enclosing them in quotes as though they are strings; whereas, conversely, I'm guessing that image is a text but you are calling it as a literal.

I am sure other respondents will tell you not to pass values directly in the query but as parameters; but that is for safety and is not the issue you are struggling with (and it is many years since I used PHP so I cannot remember how it passes as parameters).
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900