首页 > 解决方案 > 显示表格数据

问题描述

我正在创建一个待办事项列表来添加任务、删除任务并设置每个任务的优先级。但是,我不断收到此错误:在此处输入图像描述

主要问题似乎与第 57 行的 while 循环有关,但我会将所有代码放在上面,以防问题的根源更进一步。

<?php
//Creating variables for connection parameters
$servername = "localhost";
$username = "root";
$password = "";
$database = "mydb";

//Create connection
$connection = mysqli_connect($servername,$username, $password, $database);

//Check connection
if(!$connection){ //if the connection failed
    die("Connection failed:" . mysqli_connect_error());
}
echo "Connected successfully! </br>"; // if it connected succesfully


if(isset($_POST['submit']) ){ //if the submit button is clicked
    $name = $_POST['name']; //Creating variable $name constaining the task entered
    $priority = $_POST['priority']; //Creating variable $priority constaining the priority entered
    $stmt = mysqli_prepare($connection, "INSERT INTO todos (name, priority) VALUES(?, ?)"); //Inserting the 2 values into the todo table
    mysqli_stmt_bind_param($stmt, 'si', $name, $priority); //setting the task and priority as the 2 values mentioned above
    mysqli_stmt_execute($stmt); //executing the above
}elseif(isset($_POST['delete'])){ //if the delete button is clicked
    $id = $_POST['id']; //Creating variable $id constaining the id of the task entered
    $stmt = mysqli_prepare($connection, "DELETE FROM todos WHERE id = ?"); //deleting the task containing an ____ id
    mysqli_stmt_bind_param($stmt, 'i', $id); // setting the above ___ id to the id of the task wanting to be deleted
    mysqli_stmt_execute($stmt); //executing the above
}
?>

<!DOCTYPE HTML> <!--Starting html part-->
<html lang="en"> <!--Setting page language to english-->
<head>
    <title>Todo List</title> <!--title of webpage-->
    <!--Importing the required fonts for the page-->
    <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic">
    <link rel="stylesheet" href="//cdn.rawgit.com/necolas/normalize.css/master/normalize.css">
    <link rel="stylesheet" href="//cdn.rawgit.com/milligram/milligram/master/dist/milligram.min.css">
</head>

<body class="container">
    <h1>Todo List</h1> <!--Title at top of page-->
    <form method="POST" action=""> <!--Creating a form thats data is going to be sent by POST method-->
        <input type="text" name="name" value=""> <!--Task bar-->
        <label for="priority">Task Priority (between 1 and 3):</label> <!--Instructions for next input box-->
        <input type="number" name="priority" min="1" max="3" style="width: 7em"> <!--Priority box from 1-3-->
        <input type="submit" name="submit" value="Add"><!--Add button-->
    </form>
    <h2>Current Todos</h2> <!--Naming table below-->
    <table class="table table-striped">
        <thead><th>Priority</th><th>Task</th><th></th></thead> <!--Naming columns. third column header is blank because it's the delete button column-->
        <tbody>
<?php
    $stmt = mysqli_prepare($connection, "SELECT * FROM todos ORDER BY id DESC"); //Ordering table from 1st task entered to last     

    while ($row = mysqli_fetch_array($stmt)){ //Loops through all the tasks in the database
?>
            <tr>
                <td><?php echo $row['priority'] ?></td><!--printing each priority to the row -->
                <td><?php echo $row['name'] ?></td> <!--printing each task to the row -->
                <td>
                    <form method="POST"> <!--Creating a form thats data is going to be sent by POST method-->
                        <button type="submit" name="delete">Delete</button> <!--Makes the delete button-->
                        <input type="button" name="id" value="<?php echo $row ['id'] ?>"> 
                        <input type="hidden" name="delete" value="true">
                    </form>
                    
                </td>
            </tr> 
<?php 
    } //Closes while loop
?> 

        </tbody>
    </table>
</body>
</html>

标签: phpmysqlmysqli

解决方案


您的错误是您从未执行过SELECT准备好的语句,而是试图直接从中获取。不幸的是,mysqli 并不像 PDO 那样简单。它不是为初学者设计的,你必须做更多的工作才能得到结果。

尽可能将 PHP 代码和 HTML 分开是一个非常好的主意。不要在 HTML 代码中创建准备好的语句。看看我在 PHP 代码末尾添加的准备好的语句,它展示了如何从准备好的语句中获取结果。

PHP

<?php
//Creating variables for connection parameters
$servername = "localhost";
$username = "root";
$password = "";
$database = "mydb";

// Enable error reporting
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
//Create connection
$connection = mysqli_connect($servername, $username, $password, $database);

if (isset($_POST['submit'])) { //if the submit button is clicked
    $name = $_POST['name']; //Creating variable $name constaining the task entered
    $priority = $_POST['priority']; //Creating variable $priority constaining the priority entered
    $stmt = mysqli_prepare($connection, "INSERT INTO todos (name, priority) VALUES(?, ?)"); //Inserting the 2 values into the todo table
    mysqli_stmt_bind_param($stmt, 'si', $name, $priority); //setting the task and priority as the 2 values mentioned above
    mysqli_stmt_execute($stmt); //executing the above
} elseif (isset($_POST['delete'])) { //if the delete button is clicked
    $id = $_POST['id']; //Creating variable $id constaining the id of the task entered
    $stmt = mysqli_prepare($connection, "DELETE FROM todos WHERE id = ?"); //deleting the task containing an ____ id
    mysqli_stmt_bind_param($stmt, 'i', $id); // setting the above ___ id to the id of the task wanting to be deleted
    mysqli_stmt_execute($stmt); //executing the above
}

$stmt = mysqli_prepare($connection, "SELECT * FROM todos ORDER BY id DESC"); //Ordering table from 1st task entered to last
mysqli_stmt_execute($stmt); //executing the above
$result = mysqli_stmt_get_result($stmt);
// Optionally fetch it as an array
// $data = mysqli_fetch_all($result);
?>

HTML:

<!DOCTYPE HTML>
<!--Starting html part-->
<html lang="en">
<!--Setting page language to english-->

<head>
    <title>Todo List</title>
    <!--title of webpage-->
    <!--Importing the required fonts for the page-->
    <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic">
    <link rel="stylesheet" href="//cdn.rawgit.com/necolas/normalize.css/master/normalize.css">
    <link rel="stylesheet" href="//cdn.rawgit.com/milligram/milligram/master/dist/milligram.min.css">
</head>

<body class="container">
    <h1>Todo List</h1>
    <!--Title at top of page-->
    <form method="POST" action="">
        <!--Creating a form thats data is going to be sent by POST method-->
        <input type="text" name="name" value="">
        <!--Task bar-->
        <label for="priority">Task Priority (between 1 and 3):</label>
        <!--Instructions for next input box-->
        <input type="number" name="priority" min="1" max="3" style="width: 7em">
        <!--Priority box from 1-3-->
        <input type="submit" name="submit" value="Add">
        <!--Add button-->
    </form>
    <h2>Current Todos</h2>
    <!--Naming table below-->
    <table class="table table-striped">
        <thead>
            <th>Priority</th>
            <th>Task</th>
            <th></th>
        </thead>
        <!--Naming columns. third column header is blank because it's the delete button column-->
        <tbody>
            <?php
            foreach ($result as $row) : //Loops through all the tasks in the database
            ?>
                <tr>
                    <td><?php echo $row['priority'] ?></td>
                    <!--printing each priority to the row -->
                    <td><?php echo $row['name'] ?></td>
                    <!--printing each task to the row -->
                    <td>
                        <form method="POST">
                            <!--Creating a form thats data is going to be sent by POST method-->
                            <button type="submit" name="delete">Delete</button>
                            <!--Makes the delete button-->
                            <input type="button" name="id" value="<?php echo $row['id'] ?>">
                            <input type="hidden" name="delete" value="true">
                        </form>

                    </td>
                </tr>
            <?php
            endforeach;
            ?>
        </tbody>
    </table>
</body>
</html>

推荐阅读