Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Fatal error: Uncaught mysqli_sql_exception: 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 ' , 2)' at line 1 in C:\xampp\htdocs\macibas_4p\formsKD\services\order_item_services.php:46 Stack trace: #0 C:\xampp\htdocs\macibas_4p\formsKD\services\order_item_services.php(46): mysqli->query('INSERT INTO ord...') #1 C:\xampp\htdocs\macibas_4p\formsKD\index.php(72): OrderItemServices->insertOrderItem('Juris Priede', '28741234', '23.11.2022', 'Razer pele 24.1...', '2') #2 {main} thrown in C:\xampp\htdocs\macibas_4p\formsKD\services\order_item_services.php on line 46
PHPinsertAction
<pre><?php
                if(isset($_POST['add'])){
                    $orderData = $_POST['order'];
                    $productData = $_POST['product'];
                    
                    $fName = explode(" ", $orderData)[0];
                    $lName = explode(" ", $orderData)[1];
                    
                    $customer = $fName." ".$lName;
                    
                    $customerPhone = explode(" ", $orderData)[2];
                    $orderDate = explode(" ", $orderData)[3];
                    $qnt = $_POST['quantity'];
                    $p = $_POST['product'];
                    $product = explode(" ", $productData)[0];
                    
                    
                   	$result = $orderItemService->insertOrderItem($customer, $customerPhone, $orderDate, $p, $qnt);
                        
                    if($result){
                        echo "<p>Order item data insertion has been successful</p><br>";
                    }else{
                        echo "<p>Order item data insertion hasnt been successful</p><br>";
                    }
                
                }
            ?>
ServiceClassInsert
<pre>public function insertOrderItem($customer, $phone, $orderDate, $product, $quantity){
            if($customer == null || $phone == null || $orderDate == null || $product == null){
                return false;
            }
            
            $orderItems = $this->selectAllOrderItems();
            
            if(sizeof($orderItems) > 0){
               foreach($orderItems as $orderItem){
                   $customerName = $orderItem->getOrder()->getCustomer()->getName();
                   $customerPhone = $orderItem->getOrder()->getCustomer()->getPhone();
                   
                   $orderDateCheck = date_create($orderDate);
                   $orderDateCheck = date_format($orderDateCheck, 'd.m.Y');
                   
                   $isOrder = $customerName == $customer && 
                       $customerPhone == $phone &&
                       $orderDateCheck == $orderItem->getOrder()->getOrderDate();
                   
                   $productName = $orderItem->getProduct()->getName();
                   $isProduct = $productName == $product;
                   
                   if($isOrder && $isProduct){
                       return false;
                   }
               } 
            }
            
            $orderService = new OrderServices();
            $orderID = $orderService->selectOrderID($customer, $phone, $orderDate);
            
            $productService = new ProductServices();
            $productID = $productService->selectProductID($product);
            
            if($quantity == null)
                $quantity = 1;
            
            $sql = "INSERT INTO ".$this->table."(order_id, product_id, quantity) VALUES ($orderID, $productID, $quantity)";

            $isInserted = $this->conn->query($sql);

            if($isInserted){
                return true;
            }
            else{
                return false;
            }
        }


What I have tried:

I have tried multiple different things. Tried to insert data manually it needs for insertion into db and tried to output it in var_dump. Looked at different solution online but still nothing.
Posted
Updated 27-Nov-22 0:49am

1 solution

Don't do it like that! 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?

Fix that through your whole app, and the problem you can see will disappear at the same time.
 
Share this answer
 

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