简体   繁体   中英

Selecting from table and inserting in another table

Firstly I apologize for my bad english (its not my primary language)

This code is supposed to select to show details about the order by selecting the id of the transaction from orders page and then it will search the database from order_items table and displaying them to the user. I have no problems with this if it was information I need to display from the same table. The only difference here is that it should be displayed from another table. (I have set the foreign key to order_id).

index.php

<a data-toggle="modal" data-target=".bs-example-modal-lg" href="viewform.php?view_id=<?php echo $row['id']; ?>">View Order Details</a>

viewform.php

if(isset($_GET['view_id']) && !empty($_GET['view_id']))
{
    $id = $_GET['view_id'];
    $stmt_edit = $DB_con->prepare('SELECT order_id, product_id, quantity FROM `order_items` WHERE id =:uid');
    $stmt_edit->execute(array(':uid'=>$id));
    $edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
    extract($edit_row);
}
else
{
    header("Location: index.php");

When I run this code I get this error:

Warning: extract() expects parameter 1 to be array, boolean given in C:\\xampp\\htdocs\\shopping_cart\\cms\\orders\\editform.php on line 13

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 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 'WHERE ID='34'' at line 4 in C:\\xampp\\htdocs\\shopping_cart\\cms\\orders\\editform.php:42 Stack trace: #0 C:\\xampp\\htdocs\\shopping_cart\\cms\\orders\\editform.php(42): PDOStatement->execute() #1 {main} thrown in C:\\xampp\\htdocs\\shopping_cart\\cms\\orders\\editform.php on line 42

found the solution.. it's a simple solution really. It should be order_id not id since it's fetching the id from the first table and validating it with the second table.

$stmt_edit = $db_con->prepare('SELECT order_id, product_id, quantity FROM order_items WHERE order_id =:uid');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM