[英]Display data from database table into a html table using php
我的數據庫“ enchere”中有一個名為Produit的表。
我想顯示存儲在表產品中的數據。 這些數據是Nom_produit,description_produit和產品圖像。
圖像存儲在服務器中(在我的機器上,我正在使用Xampp測試代碼)。
我得到所有數據並顯示出來。 問題是無法顯示圖像。 我試圖確保正確輸入圖像的URL,並且無法解決問題。 請提供任何幫助,並附上了我的網頁的屏幕截圖,下面沒有顯示圖像是我的代碼。
$conn=new mysqli('localhost','root','','enchere');
if (!$conn) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
$sql = "select * from produit ";
$query = mysqli_query($conn, $sql);
if (!$query) {
die ('SQL Error: ' . mysqli_error($conn));
}
//$row = mysqli_fetch_array($query);
//echo "127.0.0.1/images/".$row['image'];
?>
<html>
<head>
<title>Displaying MySQL Data in HTML Table</title>
<style type="text/css">
body {
font-size: 15px;
color: #343d44;
font-family: "segoe-ui", "open-sans", tahoma, arial;
padding: 0;
margin: 0;
}
table {
margin: auto;
font-family: "Lucida Sans Unicode", "Lucida Grande", "Segoe Ui";
font-size: 12px;
}
h1 {
margin: 25px auto 0;
text-align: center;
text-transform: uppercase;
font-size: 17px;
}
table td {
transition: all .5s;
}
/* Table */
.data-table {
border-collapse: collapse;
font-size: 14px;
min-width: 537px;
}
.data-table th,
.data-table td {
border: 1px solid #e1edff;
padding: 7px 17px;
}
.data-table caption {
margin: 7px;
}
/* Table Header */
.data-table thead th {
background-color: #508abb;
color: #FFFFFF;
border-color: #6ea1cc !important;
text-transform: uppercase;
}
/* Table Body */
.data-table tbody td {
color: #353535;
}
.data-table tbody td:first-child,
.data-table tbody td:nth-child(4),
.data-table tbody td:last-child {
text-align: right;
}
.data-table tbody tr:nth-child(odd) td {
background-color: #f4fbff;
}
.data-table tbody tr:hover td {
background-color: #ffffa2;
border-color: #ffff0f;
}
/* Table Footer */
.data-table tfoot th {
background-color: #e5f5ff;
text-align: right;
}
.data-table tfoot th:first-child {
text-align: left;
}
.data-table tbody td:empty
{
background-color: #ffcccc;
}
</style>
</head>
<body>
<h1>VALIDATION DES PRODUITS AJOUTES</h1>
<table class="data-table">
<caption class="title">Produit en attente Validation administrateur</caption>
<thead>
<tr>
<th>Nom_Produit</th>
<th>Description_Produit</th>
<th>Prix_Produit</th>
<th>Image_Produit</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($query))
{
//$amount = $row['amount'] == 0 ? '' : number_format($row['amount']);
echo '<tr>
<td>'.$row['nom'].'</td>
<td>'.$row['description'].'</td>
<td>'.$row['prix'].'</td>
<td>'.'<img src='."127.0.0.1/images/".$row['image']." width=100 height=100";'/></td>
</tr>';
}?>
</tbody>
</table>
</body>
</html>
<?php
while ($row = mysqli_fetch_array($query)){
//$amount = $row['amount'] == 0 ? '' : number_format($row['amount']);
echo '<tr>
<td>'.$row['nom'].'</td>
<td>'.$row['description'].'</td>
<td>'.$row['prix'].'</td>
<td><img src="/images/'. $row['image'] . '" width="100" height="100"/></td>
</tr>';
}
?>
您的圖片路徑應該是相對的,而不是127.0.0.1! 如果前面的松弛情況弄亂了,也可以嘗試src =“ images /”。 您也可以執行$ _SERVER ['HTTP_HOST']之類的操作來獲取URL的主機名,而不是使用127.0.0.1這樣的IP :)
我檢測到的另一個問題是$ row ['image']值的串聯問題...您弄亂了'和'符號,所以這可能就是為什么它根本不顯示的原因。我相信上面的代碼可以解決您的問題!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.