【问题标题】:Display data from database table into a html table using php使用php将数据库表中的数据显示到html表中
【发布时间】:2018-04-09 22:33:20
【问题描述】:

我的数据库“enchere”中有一个名为 Produit 的表。

我想显示存储在表 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>

【问题讨论】:

  • "我得到所有数据并显示。问题是图像无法显示"数据库中的图像链接是否正确?图像的位置是否正确?这个输出var_dump($row['image']); 是什么?表格中是否存在“图像”字段?
  • OP 在这里搞乱了连接...如您所见,他在单引号和双引号之间切换,因此最后一个 单元格存在问题...此外,图片链接不是相对的。 '.'
  • 我要感谢你们所有人 :) :) 它有效

标签: php html image


【解决方案1】:
<?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'] 值的连接存在问题...您弄乱了那里的 ' 和 " 符号,所以这可能就是它根本没有出现的原因。不管怎样,我相信我上面的代码可以解决你的问题!

【讨论】:

    猜你喜欢
    • 2018-04-25
    • 2014-01-15
    • 2017-08-15
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多