Php 表单未向我的数据库添加数据

标签 php html mysql forms

我有这个表单,它旨在接受用户输入,然后一旦按下提交,它就会将值存储在数据库中。由于某种原因,按下提交后,用户会被重定向到 View 页面,但数据不会插入到数据库中。

这是添加记录代码:

 <?php
    /*
        Allows the user to both create new records and edit existing records
    */

    // connect to the database
    include("connection.php");

    // creates the new/edit record form
    // since this form is used multiple times in this file, I have made it a function that is easily    reusable
    function renderForm($memberID = '', $username = '', $password ='', $firstname ='', $lastname ='', $address ='', $email ='', $error = '')
    { ?>
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
        <html>
            <head>  
                <title>
                <?php if ($memberID != '') { echo "Edit Record"; } else { echo "New Record"; } ?>
                </title>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
            </head>
            <body>
                <h1><?php if ($memberID != '') { echo "Edit Record"; } else { echo "New Record"; } ?></h1>
                <?php if ($error != '') {
                    echo "<div style='padding:4px; border:1px solmemberID red; color:red'>" . $error
                        . "</div>";
                } ?>

                <form action= ""  method="post">
                <div>
                    <?php if ($memberID != '') { ?>
                        <input type="hidden" name="memberID" value="<?php echo $memberID; ?>" />
                        <p>MemberID: <?php echo $memberID; ?></p>
                    <?php } ?>

                    <strong>Username: *</strong> <input type="text" name="username" value="<?php echo $username; ?>"/><br/>
                    <strong>Password: *</strong> <input type="password" name="password" value="<?php echo $password; ?>"/><br/>
                    <strong>First Name: *</strong> <input type="text" name="firstname" value="<?php echo $firstname; ?>"/><br/>
                    <strong>Last Name: *</strong> <input type="text" name="lastname" value="<?php echo $lastname; ?>"/><br/>
                    <strong>Address: *</strong> <input type="text" name="address" value="<?php echo $address; ?>"/><br/>
                    <strong>Email: *</strong> <input type="text" name="email" value="<?php echo $email; ?>"/><br/>
                    <p>* required</p>
                    <input type="submit" name="submit" value="Submit" />
                </div>
                </form>
            </body>
        </html>

    <?php }

        /*

           NEW RECORD

        */
    {
        // if the form's submit button is clicked, we need to process the form
        if (isset($_POST['submit']))
        {
            // get the form data
                $username = htmlentities($_POST['username'], ENT_QUOTES);
                $password = htmlentities($_POST['password'], ENT_QUOTES);
                $firstname = htmlentities($_POST['firstname'], ENT_QUOTES);
                $lastname = htmlentities($_POST['lastname'], ENT_QUOTES);
                $address = htmlentities($_POST['address'], ENT_QUOTES);
                $email = htmlentities($_POST['email'], ENT_QUOTES);

            // check that firstname and lastname are both not empty
            if ($username == '' || $password == '' || $firstname == '' || $lastname == '' || $address == '' || $email == '')
            {
                // if they are empty, show an error message and display the form
                $error = 'ERROR: Please fill in all required fields!';
                    renderForm($username, $password, $firstname, $lastname, $address, $email, $error);
            }
            else
            {
                // insert the new record into the database
                if ($stmt = $mysqli->prepare("INSERT into members (username, password, firstname, lastname, address, email) VALUES (?, ?, ?, ?, ?, ?)"))
                {
                    $stmt->bind_param($username, $password, $firstname, $lastname, $address, $email, $error, $memberID);
                    $stmt->execute();
                    $stmt->close();
                }
                // show an error if the query has an error
                else
                {
                    echo "ERROR: Could not prepare SQL statement.";
                }

                // redirec the user
                header("Location: view.php");
            }

        }
        // if the form hasn't been submitted yet, show the form
        else
        {
            renderForm();
        }
    }

    // close the mysqli connection
    $mysqli->close();

    ?>

这是查看页面:

<!DOCTYDOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
        <head>  
                <title>View Records</title>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        </head>
        <body>

                <h1>View Records</h1>

                <p><b>View All</b> | <a href="view-paginated.php">View Paginated</a></p>

                <?php


                        // connect to the database
                        include('connection.php');

                        // get the records from the database
                        if ($result = $mysqli->query("SELECT * FROM members ORDER BY memberID"))
                        {
                                // display records if there are records to display
                                if ($result->num_rows > 0)
                                {
                                        // display records in a table
                                        echo "<table border='1' cellpadding='10'>";

                                        // set table headers
                                        echo "<tr><th>memberID
                                        </th><th>username
                                        </th><th>password
                                        </th><th>firstname
                                        </th><th>lastname
                                        </th><th>address
                                        </th><th>email";

                                        while ($row = $result->fetch_object())
                                        //print "<pre>"; print_r($row); exit;
                                        {
                                                // set up a row for each record
                                                echo "<tr>";
                                                echo "<td>" . $row->memberID . "</td>";
                                                echo "<td>" . $row->username . "</td>";
                                                echo "<td>" . $row->password . "</td>";
                                                echo "<td>" . $row->firstname . "</td>";
                                                echo "<td>" . $row->lastname . "</td>";
                                                echo "<td>" . $row->address . "</td>";
                                                echo "<td>" . $row->email . "</td>";
                                                echo "<td><a href='edit.php?memberID=" . $row->memberID . "'>Edit</a></td>";
                                                echo "<td><a href='delete.php?memberID=" . $row->memberID . "'>Delete</a></td>";
                                                echo "</tr>";
                                        }

                                        echo "</table>";
                                }
                                // if there are no records in the database, display an alert message
                                else
                                {
                                        echo "No results to display!";
                                }
                        }
                        // show an error if there is an issue with the database query
                        else
                        {
                                echo "Error: " . $mysqli->error;
                        }

                        // close database connection
                        $mysqli->close();


                ?>

                <a href="addrecord.php">Add New Record</a>
        </body>
</html>

最佳答案

第一个错误是使用不正确的参数调用bind_param。请参阅 mysqli_stmt_bind_param 的文档

另一个错误是需要绑定(bind)的参数数量(通过使用 prepare() 构建的 sql 查询进行绑定(bind),这与使用 bind_param 绑定(bind)的参数数量不同。

我还建议您替换行 $stmt->***; 以添加更多错误检查点

$res = $stmt->bind_param(/* correct your code according to the doc :) */);
if (!$res)
  echo 'error when binding params : '.$stmt->error;
else
{
  $res = $stmt->execute();
  if (!$res)
    echo 'error at stmt->execute() '.$stmt->error;
}

关于Php 表单未向我的数据库添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26303140/

相关文章:

html - 如果我在我的页面上添加 ' &lt;meta http-equiv="X-UA-Compatible"content ="IE=edge,chrome=1">' 有什么副作用吗?

python - 通过python使用密码执行unix命令

php - 如何仅在值为空时更新,否则在下一行中更新

javascript - 如何将php字符串保存到javascript

javascript - 如何让 Google 表格绑定(bind) code.js 从未包含的远程 .js 文件读取 JavaScript 并对其执行操作?

javascript - 如何将 HTML 转为 JPG/PNG?在 Javascript/Typescript 中

php - 通过列的第一个字母从 MySQL 表中选择 COUNT

PHP:对象数组和内存泄漏

php - 如何根据要求在一天中给出条件

php jquery 显示来自 mysql getjson 的数据