在表中显示数据库的内容

时间:2017-11-02 19:44:36

标签: php sql pdo

我无法在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查询运行正常。

2 个答案:

答案 0 :(得分:1)

代码中的问题:

  1. 如果您正在使用p repared statements,那么您还应该将输入值正确绑定到输入标记。否则,准备SQL语句没有任何意义,并且您完全接触SQL injection
  2. 您在html中写了<thread>而不是<thead>
  3. <thead>必须在<tbody>之前关闭。
  4. <thead><tr><th>
  5. 您错过了在td s <td><?php echo $id; ?></td>内写“php”的内容:<?#php endwhile ?>
  6. }被错误地放置。完全删除它。
  7. <td><? echo $description; }?></td>中的Undefined index位置错误。删除它。
  8. 您可能会收到表格中echo的{​​{1}}次通知。检查isset
  9. 我的代码提案:

    • 首先激活error reporting,以确保不会引发任何错误,从而导致html表中缺少记录。
    • 仅使用一个带有多个提交按钮的表单。所有按钮都具有相同的name属性。每个按钮的相应供应标识为value属性。
    • 不使用PHP代码来处理<head> index.php标记中的表单提交或数据包提取操作。放在页面的开头。
    • 请勿使用Location标头。不好的主意。
    • 获取PHP数组中的所有数据库数据,并在HTML代码中读取它们。
    • 使用fetchAll()而不是while循环fetch()
    • 始终将我提供的三个<meta>标记放在您的html / php页面的<head>标记内。
    祝你好运。

    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>
    

答案 1 :(得分:0)

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

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

  • 在HTML已经显示后,您有header重定向,因此标题已经发送。这应该移动到文件的顶部(或者最好是另一个文件)。
  • <thead>封装了表格的头部,而不是<thread>
  • 请务必关闭所有代码(<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); // 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 -->