PHP无法显示数据库中的所有必需数据

时间:2017-04-30 16:54:51

标签: php mysql 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');
?>

表格外观的截图 enter image description here

1 个答案:

答案 0 :(得分:0)

您正在已经使用它的循环中重用$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>';
    }