Php 在表中显示数据库的内容

Php 在表中显示数据库的内容,php,sql,pdo,Php,Sql,Pdo,我在html表格中显示数据库内容时遇到问题。 这个表是创建的,但它只有标题,我确实发现另一个线程有相同的问题,但它似乎是一个不同的问题为我 <!DOCTYPE html> <html> <head> <?php require_once 'db.php'; if(isset($_POST['cyanxerox'])){$id = 1; } if(isset($_POST['magenta

我在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>

主页
ICT支持打印机用品库存
//这是不起作用的部分
身份证件
名称
库存数量
描述
编辑:我将代码添加为整个文件,
我还可以说sql查询运行得很好。

最佳实践是将PHP和HTML分开,但现在忽略这一点,问题是循环和输出没有连接

您有几个问题,您应该检查一些HTML结构,并已被更正和注释

  • 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 -->

主页
ICT支持打印机用品库存
//这是不起作用的部分
身份证件
名称
库存数量
描述

代码中的问题:

  • 如果使用p,则还应将输入值适当地绑定到输入标记。否则,准备SQL语句是没有意义的,您将完全暴露在这些SQL语句中
  • 您用html编写了
    而不是
  • 必须在
    之前关闭
  • 之后是
    而不是
  • 您没有在
    td
    s:
    中编写“php”
  • 放置错误。完全移除它
  • 您在
    中错误地放置了
    }
    。移除它
  • 对于表体中的
    echo
    s,您可能会收到
    Undefined index
    通知。检查它们是否设置了
    isset
  • 我的代码提案:

    • 首先激活,以确保不会引发错误,从而导致html表中缺少记录
    • 只使用一个表单和多个提交按钮。所有按钮都具有相同的
      名称
      属性。每个按钮都有相应的电源id,即
      属性
    • index.PHP
      标记中不放置用于处理表单提交或db获取操作的PHP代码。放在这一页的开头
    • 不要使用该
      位置
      标题。坏主意
    • 获取PHP数组中的所有db数据,并在HTML代码中读取它们
    • 使用
      fetchAll()
      而不是
      循环
      fetch()
    • 始终将我提供的三个
      标记放在html/php页面的
      标记中
    祝你好运

    db.php
    
    主页
    ICT支持打印机用品库存
    青色复印机
    洋红复印机
    黑色复印机
    黄色复印机
    

    身份证件 名称 库存数量 描述
    您的表格html和db记录获取代码在两个单独的文件中,或者在同一个文件中?我编辑了这篇文章以包含整个php文件。抱歉,如果它没有被很好地编码,我是一个noob:P@alivetodie在你的while循环中,你只是给变量赋值,就是这样。while循环结束后,变量将消失,因为它们不在while循环的范围内。您不需要那么多表单。您在这里遇到了一个范围问题,应该使用错误报告,您将看到这里发生了什么。
    <?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
            )
    );
    
    <?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>