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";

?>

然后我得到的结果是没有显示“您的数据中没有产品列表”,我该如何解决!

4

3 回答 3

0

将最后一个分配更改为:

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

您正在用该错误消息替换从数据库中检索到的所有结果。

<?php echo ... ?>你在作业中所做的所有地方都$product_list应该是连接——你只能在内联 HTML 中使用它,而不是在字符串中。

您可能不想<table>为数据库中的每一行开始一个新的。通常只有一个 HTML 表,并且每个数据库行对应于其中的一个 HTML 行。

于 2013-10-10T22:22:17.007 回答
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;
?>
于 2013-10-11T12:59:42.503 回答
0

也许是特权问题。但首先评论最后一行并尝试一下。

于 2013-10-10T22:25:58.180 回答