【问题标题】:display contents of database in table在表中显示数据库的内容
【发布时间】:2017-11-02 19:44:36
【问题描述】:

我无法在 html 表中显示我的数据库内容。 该表已创建,但只有标题,我确实找到了另一个有相同问题的线程,但对我来说似乎是另一个问题。

<!DOCTYPE html>
<html>
    <head>
    <?php 
        require_once 'db.php';

        if(isset($_POST['cyanxerox'])){$id = 1; }
        if(isset($_POST['magentaxerox'])){$id = 2;}
        if(isset($_POST['blackxerox'])){$id = 3;}
        if(isset($_POST['yellowxerox'])){$id = 4;}

        if(isset($id)){
            $sth = $conn->prepare("UPDATE supplies SET quantity = quantity + 1 WHERE Id=".$id);
            $sth->execute();
            header('Location: index.php');
            die("Posted, now redirecting");
        }

        #this is the part that is not working
        $result = $conn->prepare('SELECT id, name_of_supply, quantity, description from supplies');
        $result->execute();

        while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
            $id= $row['id'];
            $name_of_supply =  $row['name_of_supply'];
            $quantity = $row['quantity'];
            $description = $row['description']; 
        }




    ?>
    <title>Homepage</title> 
    <link rel="stylesheet" type="text/css" href="style/main.css">
</head>
<body>
    <h1>ICT Support Printer Supplies Inventory</h1>
    <form method="POST" action="index.php">
        <input type="submit" name="cyanxerox" value="Cyan Xerox"/>
    </form>
    <form method="POST" action="index.php">
        <input type="submit" name="magentaxerox" value="Magenta Xerox"/>
    </form>
    <form method="POST" action="index.php">
        <input type="submit" name="blackxerox" value="Black Xerox"/>
    </form>
    <form method="POST" action="index.php">
        <input type="submit" name="yellowxerox" value="Yellow Xerox"/>
    </form>
    //this is the part that is not working
    <table>
        <thread>
            <th>
                <th>ID</th>
                <th>Name</th>
                <th>Number in Stock</th>
                <th>Description</th>
            </th>
            <tbody>
                <tr>
                   <td><? echo $id; ?></td>
                   <td><? echo $name_of_supply; ?></td>
                   <td><? echo $quantity; ?></td>
                   <td><? echo $description; }?></td>
                </tr>
                   <?#php endwhile ?>
            </tbody>
        </thread>
    </table>
</body>

编辑:我将代码作为整个文件添加, 也可以说sql查询运行良好。

【问题讨论】:

  • 您的表格 html 和您的数据库记录获取代码在两个单独的文件中还是在同一个文件中?
  • 我已经编辑了帖子以包含整个 php 文件。抱歉,如果编码不好,我是菜鸟:P @AlivetoDie
  • 在你的while循环中,你只是给变量赋值,就是这样。 while循环结束后,变量会消失,因为它们超出了while循环的范围。
  • 你不需要那么多表格。
  • 你在这里遇到了一个范围问题,应该使用错误报告,你会看到这里发生了什么。

标签: php sql pdo


【解决方案1】:

代码中的问题:

  1. 如果您使用的是 prepared statements,那么您还应该正确地将输入值绑定到输入标记。否则,准备 SQL 语句是没有意义的,您将完全暴露于SQL injection
  2. 您在 html 中写了 &lt;thread&gt; 而不是 &lt;thead&gt;
  3. &lt;thead&gt;必须在&lt;tbody&gt;之前关闭。
  4. &lt;thead&gt; 之后是&lt;tr&gt; 而不是&lt;th&gt;
  5. 你错过了在tds 中写“php”:&lt;td&gt;&lt;?php echo $id; ?&gt;&lt;/td&gt;
  6. &lt;?#php endwhile ?&gt; 被错误放置。完全删除它。
  7. 您在&lt;td&gt;&lt;? echo $description; }?&gt;&lt;/td&gt; 中有一个错误的}。删除它。
  8. 您可能会在表格正文中收到Undefined index 通知echos。查看isset

我的代码建议:

  • 首先激活error reporting,以确保不会引发错误,导致html表中没有记录。
  • 仅使用一个带有多个提交按钮的表单。所有按钮都具有相同的name 属性。每个按钮都有对应的供应 id 作为value 属性。
  • index.php&lt;head&gt; 标记中不要放置用于处理表单提交或数据库获取操作的PHP 代码。放在页面的开头。
  • 不要使用Location 标头。坏主意。
  • 获取 PHP 数组中的所有 db 数据并在 HTML 代码中读取它们。
  • 使用fetchAll() 而不是while 循环与fetch()
  • 始终将我提供的三个 &lt;meta&gt; 标记放在您的 html/php 页面的 &lt;head&gt; 标记中。

祝你好运。

db.php

<?php

// Create the db connection.
$conn = new PDO(
        'mysql:host=localhost;port=3306;dbname=tests;charset=utf8'
        , 'user'
        , 'pass'
        , array(
    // Important! Research on the subject "PDO::ERRMODE_EXCEPTION".
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_EMULATE_PREPARES => FALSE,
    PDO::ATTR_PERSISTENT => TRUE
        )
);

index.php

<?php
require_once 'db.php';

/*
 * =========================
 * Activate error reporting.
 * =========================
 */
error_reporting(E_ALL);

// Set to 0 on the live server!
ini_set('display_errors', 1);

/*
 * ====================================
 * Run operations upon form submission.
 * ====================================
 */
if (isset($_POST['submitButton'])) {
    $id = $_POST['submitButton'];

    /*
     * ======================
     * Update quantity by id.
     * ======================
     */
    $sth = $conn->prepare('UPDATE supplies SET quantity = quantity + 1 WHERE Id = :id');
    $sth->execute(array(
        'id' => $id
    ));

    /*
     * ===============
     * Fetch supplies.
     * ===============
     */
    $sth = $conn->prepare('SELECT id, name_of_supply, quantity, description from supplies');
    $sth->execute();
    $supplies = $sth->fetchAll(PDO::FETCH_ASSOC);
}
?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
        <meta charset="UTF-8" />
        <!-- The above 3 meta tags must come first in the head -->

        <title>Homepage</title> 

        <link rel="stylesheet" type="text/css" href="style/main.css">
    </head>
    <body>

        <h1>ICT Support Printer Supplies Inventory</h1>

        <form method="POST" action="index.php">
            <button type="submit" name="submitButton" value="1">Cyan Xerox</button>
            <button type="submit" name="submitButton" value="2">Magenta Xerox</button>
            <button type="submit" name="submitButton" value="3">Black Xerox</button>
            <button type="submit" name="submitButton" value="4">Yellow Xerox</button>
        </form>

        <br/><br/>

        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Number in Stock</th>
                    <th>Description</th>
                </tr>
            </thead>
            <tbody>
                <?php
                if (isset($supplies)) {
                    foreach ($supplies as $supply) {
                        $id = $supply['id'];
                        $nameOfSupply = $supply['name_of_supply'];
                        $quantity = $supply['quantity'];
                        $description = $supply['description'];
                        ?>
                        <tr>
                            <td><?php echo $id; ?></td>
                            <td><?php echo $nameOfSupply; ?></td>
                            <td><?php echo $quantity; ?></td>
                            <td><?php echo $description; ?></td>
                        </tr>
                        <?php
                    }
                }
                ?>
            </tbody>
        </table>

    </body>
</html>

【讨论】:

    【解决方案2】:

    最好的做法是将 PHP 和 HTML 分开,但暂时忽略这一点,问题是您的循环和输出没有连接。

    您有几个问题,您应该查看一些 HTML 结构,并已进行更正和评论。

    • 在 HTML 已显示后,您有一个 header 重定向,因此标头已发送。这应该移到文件的顶部(或者最好是另一个文件)。
    • &lt;thead&gt; 封装了表头,而不是 &lt;thread&gt;
    • 确保关闭所有标签 (&lt;html&gt;)

    通过将循环移动到表格主体内部并循环每一行来纠正您来到这里的问题。鉴于您没有进行任何操作,您不需要将每个值存储到一个变量中,但无论哪种方式都可以。

    以下更正代码:

    <!DOCTYPE html>
    <html>
        <head>
        <?php 
            require_once 'db.php';
    
            if(isset($_POST['cyanxerox'])){$id = 1; }
            if(isset($_POST['magentaxerox'])){$id = 2;}
            if(isset($_POST['blackxerox'])){$id = 3;}
            if(isset($_POST['yellowxerox'])){$id = 4;}
    
            if(isset($id)){
                $sth = $conn->prepare("UPDATE supplies SET quantity = quantity + 1 WHERE Id=".$id); // This all but defeats the point of preparing statements, use bound parameters
                $sth->execute();
                header('Location: index.php'); // This won't work since you've already sent headers with the HTML code above this...
                die("Posted, now redirecting");
            }
    
            #this is the part that is not working
            $result = $conn->prepare('SELECT id, name_of_supply, quantity, description from supplies');
            $result->execute();
    
            while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
                $id= $row['id'];
                $name_of_supply =  $row['name_of_supply'];
                $quantity = $row['quantity'];
                $description = $row['description']; 
            }
        ?>
        <title>Homepage</title> 
        <link rel="stylesheet" type="text/css" href="style/main.css">
    </head>
    <body>
        <h1>ICT Support Printer Supplies Inventory</h1>
        <form method="POST" action="index.php">
            <input type="submit" name="cyanxerox" value="Cyan Xerox"/>
        </form>
        <form method="POST" action="index.php">
            <input type="submit" name="magentaxerox" value="Magenta Xerox"/>
        </form>
        <form method="POST" action="index.php">
            <input type="submit" name="blackxerox" value="Black Xerox"/>
        </form>
        <form method="POST" action="index.php">
            <input type="submit" name="yellowxerox" value="Yellow Xerox"/>
        </form>
        //this is the part that is not working
        <table>
            <thead> <!-- thead not thread -->
                <tr> <!-- should be a row, not th -->
                    <th>ID</th>
                    <th>Name</th>
                    <th>Number in Stock</th>
                    <th>Description</th>
                </tr>
                </thead>
                <tbody>
                   <?php
                    while ($row = $result->fetch(PDO::FETCH_ASSOC)):
                        $id= $row['id'];
                        $name_of_supply =  $row['name_of_supply'];
                        $quantity = $row['quantity'];
                        $description = $row['description'];
                    <tr>
                       <td><? echo $id; ?></td>
                       <td><? echo $name_of_supply; ?></td>
                       <td><? echo $quantity; ?></td>
                       <td><? echo $description; }?></td>
                    </tr>
                       <?php endwhile; // Table loop ?>
                </tbody>
        </table>
    </body>
    </html> <!-- close your HTML tag -->
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-30
      • 2020-11-01
      相关资源
      最近更新 更多