用php和sql在html表中显示本地服务器数据的小问题

brccelvz  于 2023-02-11  发布在  PHP
关注(0)|答案(1)|浏览(98)

今天我有一个小问题,基本的,也许是有趣的事情。今天我想用sql server的数据制作表格。为此,我使用Xampp,也许是verry常见的选择。问题是与表,因为我试图显示数据与我的想法,与我的知识和结果可能是超过我想做的。也许吧。下面是我的脚本,我想提一下***我制作了一些版本的脚本***,这里只是其中之一。
HTML + PHP表格

<div class="centred-container table">
        <table class="table-results">
            <tr class="tr-table">
                <th class="th-table th-univ">Product name</th>
                <th class="th-table th-univ">Cod PLU</th>
                <th class="th-table th-univ">Cod Intern</th>
                <th class="th-table th-univ">Cod EAN/Bare</th>
                <th class="th-table th-univ">Unitate</th>
                <th class="th-table th-univ">Pret</th>
                <th class="th-table th-univ">Stoc</th>
            </tr>
            <tr class="tr-table">
              <?php
              
                while ($row = mysqli_fetch_assoc($result)) {
                    $nameP = $row["ProductName"];
                    $PLUP = $row["ProductPLU"];
                    $ICP = $row["ProductIC"];
                    $EANP = $row["ProductEAN"];
                    $UnitP = $row["ProductUnit"];
                    $PriceP = $row["ProductPrice"];
                    $StockP = $row["ProductStock"];

                    echo"<td>".$nameP."</td>
                        <td>".$PLUP."</td>
                        <td>".$ICP."</td>
                        <td >".$EANP."</td>
                        <td >".$UnitP."</td>
                        <td >".$PriceP."</td>
                        <td >".$StockP."</td> <br>";
                }
              ?>
        </table>
    </div>

PHP部分

if (isset($_POST["submit"])) {
    session_start();

    include_once 'includes/dbh.inc.php';

    $codePLU = $_POST["productPLU"];
    $codeEAN = $_POST["productEAN"];
    $codeIC = $_POST["productIC"];

    $sql = "SELECT * FROM products WHERE ProductPLU ='$codePLU' OR ProductEAN = '$codeEAN' OR ProductIC = '$codeIC';";
    $result = mysqli_query($conn, $sql);
    
    }

?>

这里我想展示一下(SELECT)数据库中的数据,并显示在html表中。问题是我尝试和方法与<td><?php echo $variable ?></td>,其中$varable = $row["column_name"]和在这种情况下,我所有的数据只是可能是第一个或最后一个或只是一行,其中应该有更多的,因为我知道我的sql查询有更多的结果(行)比我看到的一行多。使用粘贴的脚本,我的sql查询返回了所有行,但所有行都在一行上,我希望所有行都在一个经典的表中。我只想对我的小问题得到一点帮助。我知道只是在PHP网站的基本问题,但我想从你的一些情况下,或结果,在那里我想尝试使我的表没有错误的经典表一样,我上面的错误。
对不起我的英语。

gkn4icbw

gkn4icbw1#

问题出在你的HTML表格上。需要在while循环中打开和关闭每一行(<tr>)。代码如下

<table class="table-results">
            <tr class="tr-table">
                <th class="th-table th-univ">Product name</th>
                <th class="th-table th-univ">Cod PLU</th>
                <th class="th-table th-univ">Cod Intern</th>
                <th class="th-table th-univ">Cod EAN/Bare</th>
                <th class="th-table th-univ">Unitate</th>
                <th class="th-table th-univ">Pret</th>
                <th class="th-table th-univ">Stoc</th>
            </tr>
              <?php
                while ($row = mysqli_fetch_assoc($result)) {
                    $nameP = $row["ProductName"];
                    $PLUP = $row["ProductPLU"];
                    $ICP = $row["ProductIC"];
                    $EANP = $row["ProductEAN"];
                    $UnitP = $row["ProductUnit"];
                    $PriceP = $row["ProductPrice"];
                    $StockP = $row["ProductStock"];

                    echo"<tr> <td>".$nameP."</td>
                        <td>".$PLUP."</td>
                        <td>".$ICP."</td>
                        <td >".$EANP."</td>
                        <td >".$UnitP."</td>
                        <td >".$PriceP."</td>
                        <td >".$StockP."</td> </tr>";
                }
              ?>
        </table>

相关问题