2013-10-10 58 views
0

我試圖顯示從數據庫列表顯示,但運行時的代碼它說:「你有沒有在你的數據中列出的產品呢。」但是實際上從我的數據列表...不是從數據庫

<?php 
$con = new mysqli("localhost", "root", "3250", "shopone"); 


// Check connection 
if (mysqli_connect_errno($con)) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

// This block grabs the whole list of product for viewing 
$product_list = ""; 
$sql = "SELECT * FROM product ORDER BY product_id DESC"; 

$result=mysqli_query($con,$sql); 

    while($row = mysqli_fetch_array($result)){ 
      $product_id = $row["product_id"]; 
      $product_name = $row["product_name"]; 
      $product_category = $row["product_category"]; 
       $product_retail_price = $row["product_retail_price"]; 
       $product_price = $row["product_price"]; 
       $product_detail = $row["product_detail"]; 
       $product_image = $row["screenshot"]; 
       $product_thumbnail = $row["product_thumbnail"]; 
       $product_discount = $row["product_discount"]; 
      $screenshot = $row["screenshot"]; 

      $product_list .= '<table width="80%" border="1"> 
    <tr> 
    <td width="172" valign="top"> echo <img src="' . GW_UPLOADPATH . $screenshot .'" width="111" height="149" alt="<?php echo $product_name; ?>" /><br /> 
     <a href="../product_images/<?php echo $product_id; ?>.jpg">View Full Size Image</a></td> 
    <td width="85" class="product-text">' . $product_id . '</td> 
    <td width="402" class="product-text">' . $product_name . '</td> 
    <td width="108" align="center" class="product-text">' . $product_price . '</td> 
    <td width="34" align="center" class="product-text"><a rel="leanModal" href="edit_product.php?pid=' . $product_id . '">Edit</a></td> 

    <td width="56" align="center" class="product-text"><a rel="leanModal" href="product.php?deleteid=' . $product_id . '">Delete</a></td> 
     <td width="56" align="center" class="product-text"><a href="view_product.php?pid=' . $product_id . '">View</a></td> 
    </tr> 
</table> ';    

    } 

    $product_list = "You have no product listed in your data yet"; 

?> 

然後我得到的結果是沒有顯示它說「你的數據中沒有產品列表」,我該如何解決這個問題!

+0

您正在您的變量中創建您的表格,然後用該消息覆蓋它。 – Mihai

+0

另外你不需要該表中的php標籤。 – Mihai

回答

0

改變最後分配:

if (empty($product_list)) { 
    $product_list = "You have no product listed in your data yet"; 
} 

你被替換所有從該錯誤信息數據庫檢索到的結果。

您所做的所有地方$product_list作業中的<?php echo ... ?>應該是串聯 - 只能在內聯HTML中使用,而不能在字符串中使用。

您可能不想爲數據庫中的每一行開始新的<table>。通常只有一個HTML表格,每個數據庫行對應一個HTML行。

0

也許是特權問題。但首先評論最後一行並嘗試一下。

0

您正在覆蓋上一條語句中的$ product_list,這就是爲什麼您獲得最後一次給變量的值。 另外,作爲提示,在while循環中,您可以保存每條記錄的行,因爲您已經將其存儲在數組中。 最後,您可以在此之前打開表格,然後關閉它。

<?php 
$con = new mysqli("localhost", "root", "3250", "shopone"); 
// Check connection 
if (mysqli_connect_errno($con)) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
// This block grabs the whole list of product for viewing 
$sql = "SELECT * FROM product ORDER BY product_id DESC"; 
$result = mysqli_query($con,$sql); 

//you can add this check to add 'empty table' message if no result 
if(mysql_num_rows()>0) 
{  
    $product_list = '<table>';   
    while($row = mysqli_fetch_array($result)) 
    { 
     $product_list.= '<tr>'. 
         '<td>'.$row["product_id"].'</td>'. 
         // .... 
         // do the same with all rows 
         // .... 
         '<td>'.$row["screenshot"].'</td>'. 
         '</tr>';   
    } 
    $product_list.= '</table>'; 
} 
else 
{ 
    $product_list = "You have no product listed in your data yet"; 
} 
//print 
echo $product_list; 
?>