0

I'm having trouble with my sql, particulary the place order

Fatal error: Uncaught mysqli_sql_exception: Cannot add or update a child row: a foreign key constraint fails (shoe_shop.order_table, CONSTRAINT order_table_ibfk_1 FOREIGN KEY (OrderID) REFERENCES orderline_table (OrderID)) in C:\xampp\htdocs\Shoe Shop\place-order.php:23 Stack trace: #0 C:\xampp\htdocs\Shoe Shop\place-order.php(23): mysqli->query('INSERT INTO Ord...') #1 {main} thrown in C:\xampp\htdocs\Shoe Shop\place-order.php on line 23

enter image description here

here is mmy code

include "connection.php";
session_start();
$admin_id = $_SESSION["employee_id"];
$name = $_POST['customer_names'];
$contacts = $_POST['customer_contacts'];
$orderid = rand(10000, 99999);
$sql = "SELECT * FROM Inventory_Table INNER JOIN Cart_table on Inventory_Table.PRoductID =  Cart_table.PRoductID ;";
$result = $conn->query($sql);
$sql_2 = "";
$total = 0;
if ($result->num_rows > 0) {
    // output data of each row
    while ($product_data = $result->fetch_assoc()) {
        $total += $product_data["Price"] * $product_data["Quantity"];
        $line_total = $product_data["Price"] * $product_data["Quantity"];
        $product_id = $product_data["ProductID"];
        $Quantity = $product_data["Quantity"];
        $lineid = rand(10000, 99999);
        $sql_2 .= "INSERT INTO Orderline_table values($lineid, $orderid, $product_id, $Quantity, $line_total);";
    }
    $sql = "INSERT INTO Order_table values($orderid, $total, $admin_id)";
    $conn->query($sql);
    $conn->multi_query($sql_2);
}```

1 Answers1

-1

consider customer_id inside the order_table than order_id inside the customer table. Its managable on future.

Error caused because the order_id still use on other table or they no referrence value when you try to add.

caco
  • 1