PHP无法显示来自数据库的所有必需数据(PHP Unable to display all required data from Database)
我正在努力创建一个工作良好的电子商务网站。 但是,当我尝试查看创建的订单时。 显示已经在数据库中的那些数据,并且只显示特定顺序中的第一条记录。
感谢您的帮助。
码:
<html> <?php # Access session. session_start() ; # Redirect if not logged in. if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; } # Set page title and display header section. $page_title = 'currento' ; include ( 'includes/header.html' ) ; # Open database connection require ('../connect_db.php'); $uid = $_SESSION[ 'user_id' ]; #Returns all the records from the orders table $query = "SELECT * FROM orders WHERE user_id = '$uid' "; $result = mysqli_query( $dbc, $query ) ; # Display body section. echo '<body>'; #If the variable result contains rows... if ( mysqli_num_rows( $result ) > 0 ) { #Display the page header echo'<header> <h1> Your orders </h1> </header>'; #Main content displayed in a table, TR stands for Table Row #Main content displayed in a table, TR stands for Table Row echo '<table border ="1"><tr>'; #Column Headers echo '<td> <strong>Order ID</strong><br> </td>'. '<td> <strong> User_ID </strong><br> </td>'. '<td> <strong> Total Cost </strong><br> </td>'. '<td> <strong> Dispatched? </strong><br> </td>'. '<td> <strong> Order Date </strong><br> </td> </tr>'; #Returns a more detailed view of a single item in the database. while ( $row = mysqli_fetch_array( $result, MYSQLI_ASSOC )) { #TD stands for Table Data and adds a cell to the Table Row #The full stop is used as concatenation (to join two strings) echo '<td> <strong>' . $row['order_id'] .'</strong><br> </td>'. '<td> <strong>' . $row['user_id'] .'</strong><br> </td>'. '<td> <strong>' . $row['total'] .'</strong><br> </td>'. '<td> <strong>' . $row['Sent'] .'</strong><br> </td>'. '<td> <strong>' . $row['order_date'] .'</strong><br> </td>'. '<td><table>'; $oid = $row['order_id']; $Contents_Query = "SELECT * FROM order_contents WHERE order_id = '$oid' "; $Contents_result = mysqli_query( $dbc, $Contents_Query) ; while ( $content_row = mysqli_fetch_array( $Contents_result, MYSQLI_ASSOC )) { #TD stands for Table Data and adds a cell to the Table Row #The full stop is used as concatenation (to join two strings) #echo '<tr><td> <strong>' . $content_row['item_id'] .'</strong><br> </td>'. # '<td> <strong>' . $content_row['quantity'] .'</strong><br> </td> </tr>'; $iid = $content_row['item_id']; $Contents_Query = "SELECT * FROM shop WHERE item_id = '$iid' "; $Contents_result = mysqli_query( $dbc, $Contents_Query) ; echo '<td> <strong>Item ID</strong><br> </td>'. '<td> <strong> Item Name </strong><br> </td></tr>'; while ( $content_row = mysqli_fetch_array( $Contents_result, MYSQLI_ASSOC )) { #TD stands for Table Data and adds a cell to the Table Row #The full stop is used as concatenation (to join two strings) echo '<tr><td> <strong>' . $content_row['item_id'] .'</strong><br> </td>'. '<td> <strong>' . $content_row['item_name'] .'</strong><br> </td> </tr>'; } # '<td> <strong>' . $content_row['total'] .'</strong><br> </td>'. # '<td> <strong>' . $content_row['Sent'] .'</strong><br> </td>'. # '<td> <strong>' . $content_row['order_date'] .'</strong><br> </td>'. # '</tr>'; } echo '</table>'; #Ends the table row. echo '</tr>'; } echo '</table>'; # Close database connection. mysqli_close( $dbc ) ; } else { echo '<p>There are currently no items in order_contents.</p>' ; } // This is order contents include('includes/footer.html'); ?>表格外观的屏幕截图
I am trying to create a nicely working e-commerce website. However, when I try to view the created orders. The ones that are already in the database and only the first record in the specific order is displayed.
Thank you for your help.
Code:
<html> <?php # Access session. session_start() ; # Redirect if not logged in. if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; } # Set page title and display header section. $page_title = 'currento' ; include ( 'includes/header.html' ) ; # Open database connection require ('../connect_db.php'); $uid = $_SESSION[ 'user_id' ]; #Returns all the records from the orders table $query = "SELECT * FROM orders WHERE user_id = '$uid' "; $result = mysqli_query( $dbc, $query ) ; # Display body section. echo '<body>'; #If the variable result contains rows... if ( mysqli_num_rows( $result ) > 0 ) { #Display the page header echo'<header> <h1> Your orders </h1> </header>'; #Main content displayed in a table, TR stands for Table Row #Main content displayed in a table, TR stands for Table Row echo '<table border ="1"><tr>'; #Column Headers echo '<td> <strong>Order ID</strong><br> </td>'. '<td> <strong> User_ID </strong><br> </td>'. '<td> <strong> Total Cost </strong><br> </td>'. '<td> <strong> Dispatched? </strong><br> </td>'. '<td> <strong> Order Date </strong><br> </td> </tr>'; #Returns a more detailed view of a single item in the database. while ( $row = mysqli_fetch_array( $result, MYSQLI_ASSOC )) { #TD stands for Table Data and adds a cell to the Table Row #The full stop is used as concatenation (to join two strings) echo '<td> <strong>' . $row['order_id'] .'</strong><br> </td>'. '<td> <strong>' . $row['user_id'] .'</strong><br> </td>'. '<td> <strong>' . $row['total'] .'</strong><br> </td>'. '<td> <strong>' . $row['Sent'] .'</strong><br> </td>'. '<td> <strong>' . $row['order_date'] .'</strong><br> </td>'. '<td><table>'; $oid = $row['order_id']; $Contents_Query = "SELECT * FROM order_contents WHERE order_id = '$oid' "; $Contents_result = mysqli_query( $dbc, $Contents_Query) ; while ( $content_row = mysqli_fetch_array( $Contents_result, MYSQLI_ASSOC )) { #TD stands for Table Data and adds a cell to the Table Row #The full stop is used as concatenation (to join two strings) #echo '<tr><td> <strong>' . $content_row['item_id'] .'</strong><br> </td>'. # '<td> <strong>' . $content_row['quantity'] .'</strong><br> </td> </tr>'; $iid = $content_row['item_id']; $Contents_Query = "SELECT * FROM shop WHERE item_id = '$iid' "; $Contents_result = mysqli_query( $dbc, $Contents_Query) ; echo '<td> <strong>Item ID</strong><br> </td>'. '<td> <strong> Item Name </strong><br> </td></tr>'; while ( $content_row = mysqli_fetch_array( $Contents_result, MYSQLI_ASSOC )) { #TD stands for Table Data and adds a cell to the Table Row #The full stop is used as concatenation (to join two strings) echo '<tr><td> <strong>' . $content_row['item_id'] .'</strong><br> </td>'. '<td> <strong>' . $content_row['item_name'] .'</strong><br> </td> </tr>'; } # '<td> <strong>' . $content_row['total'] .'</strong><br> </td>'. # '<td> <strong>' . $content_row['Sent'] .'</strong><br> </td>'. # '<td> <strong>' . $content_row['order_date'] .'</strong><br> </td>'. # '</tr>'; } echo '</table>'; #Ends the table row. echo '</tr>'; } echo '</table>'; # Close database connection. mysqli_close( $dbc ) ; } else { echo '<p>There are currently no items in order_contents.</p>' ; } // This is order contents include('includes/footer.html'); ?>Screenshot of how the table looks
最满意答案
您正在使用已在使用它的循环内重新使用$Contents_result 。 这将破坏外部循环。
$oid = $row['order_id']; $Contents_Query = "SELECT * FROM order_contents WHERE order_id = '$oid' "; $Contents_result = mysqli_query( $dbc, $Contents_Query) ; while ( $content_row = mysqli_fetch_array( $Contents_result, MYSQLI_ASSOC )) { #TD stands for Table Data and adds a cell to the Table Row #The full stop is used as concatenation (to join two strings) #echo '<tr><td> <strong>' . $content_row['item_id'] .'</strong><br> </td>'. # '<td> <strong>' . $content_row['quantity'] .'</strong><br> </td> </tr>'; $iid = $content_row['item_id']; $Contents_Query = "SELECT * FROM shop WHERE item_id = '$iid' "; $Contents_result2 = mysqli_query( $dbc, $Contents_Query) ; // error correction ^ echo '<td> <strong>Item ID</strong><br> </td>'. '<td> <strong> Item Name </strong><br> </td></tr>'; while ( $content_row = mysqli_fetch_array( $Contents_result2, MYSQLI_ASSOC )) { // error correction --------------------------------------------- ^ #TD stands for Table Data and adds a cell to the Table Row #The full stop is used as concatenation (to join two strings) echo '<tr><td> <strong>' . $content_row['item_id'] .'</strong><br> </td>'. '<td> <strong>' . $content_row['item_name'] .'</strong><br> </td> </tr>'; } # '<td> <strong>' . $content_row['total'] .'</strong><br> </td>'. # '<td> <strong>' . $content_row['Sent'] .'</strong><br> </td>'. # '<td> <strong>' . $content_row['order_date'] .'</strong><br> </td>'. # '</tr>'; }You are re-using $Contents_result inside a loop that already uses it. That will destroy the outer loop.
$oid = $row['order_id']; $Contents_Query = "SELECT * FROM order_contents WHERE order_id = '$oid' "; $Contents_result = mysqli_query( $dbc, $Contents_Query) ; while ( $content_row = mysqli_fetch_array( $Contents_result, MYSQLI_ASSOC )) { #TD stands for Table Data and adds a cell to the Table Row #The full stop is used as concatenation (to join two strings) #echo '<tr><td> <strong>' . $content_row['item_id'] .'</strong><br> </td>'. # '<td> <strong>' . $content_row['quantity'] .'</strong><br> </td> </tr>'; $iid = $content_row['item_id']; $Contents_Query = "SELECT * FROM shop WHERE item_id = '$iid' "; $Contents_result2 = mysqli_query( $dbc, $Contents_Query) ; // error correction ^ echo '<td> <strong>Item ID</strong><br> </td>'. '<td> <strong> Item Name </strong><br> </td></tr>'; while ( $content_row = mysqli_fetch_array( $Contents_result2, MYSQLI_ASSOC )) { // error correction --------------------------------------------- ^ #TD stands for Table Data and adds a cell to the Table Row #The full stop is used as concatenation (to join two strings) echo '<tr><td> <strong>' . $content_row['item_id'] .'</strong><br> </td>'. '<td> <strong>' . $content_row['item_name'] .'</strong><br> </td> </tr>'; } # '<td> <strong>' . $content_row['total'] .'</strong><br> </td>'. # '<td> <strong>' . $content_row['Sent'] .'</strong><br> </td>'. # '<td> <strong>' . $content_row['order_date'] .'</strong><br> </td>'. # '</tr>'; }更多推荐
发布评论