我正在尝试显示数据库中的列表但是在运行该代码时它说“您的数据中没有列出产品”,但实际上有我的数据列表......
<?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 :(得分:0)
将最后一项作业更改为:
if (empty($product_list)) {
$product_list = "You have no product listed in your data yet";
}
您正在使用该错误消息替换从数据库中检索到的所有结果。
<?php echo ... ?>
作业中$product_list
所做的所有地点都应该是连接 - 您只能在内联HTML中使用它,而不能在字符串中使用。
您可能不希望为数据库中的每一行启动新的<table>
。通常只有一个HTML表,每个数据库行对应一个HTML行。
答案 1 :(得分:0)
也许是特权问题。但首先评论最后一行并试一试。
答案 2 :(得分: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;
?>