php - 表单数据未插入数据库

标签 php mysql mysql-workbench

当我从表单提交数据时,它不会将表单数据插入到数据库中。与数据库的连接已设置,当我检查连接时,它总是响应已连接。数据库已正确设置所有值:id、名称、日期、电子邮件地址和文本。用户名、密码和数据库名称正确。

语法应该正确。我使用 mysql 工作台。

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=windows-1252">
        <title></title>
    </head>
    <body> 

    <link rel="stylesheet" type="text/css" href="input.css">

    <header>
      <h1></h1>
    </header>

    <nav>

      <ul>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
      </ul>
    </nav>

    <main>
        <form method="post" action="input.php">
            <label>Name</label>     
            <input placeholder="Name" name="name" type="text"><br>

            <label>Mailadress</label>  
            <input placeholder="Emailadress" name="eMail" type="email"><br>

            <label>Your comment</label> 
            <textarea name="comment" placeholder="Text" name="comment"cols="60" rows="15"></textarea> 

            <input value="Send" type="submit">
        </form>
        </main>
    </body>
</html>

PHP 代码:

<?php
    if($con->connect_error) 
        {
        die("No connection" .$con-> connect_error);
        }
        echo "Connected";


    $guestbook = new GuestbookAccess();

    if (isset($_POST['submit'])){
      $name = $_POST['name']; $eMail = $_POST['eMail']; $comment = $_POST['comment'];  
    }

    class GuestbookAccess
    {
        private $db;

        /**
         * Opens the database.
         */
        public function __construct()
        {
            $username = "tipuser";
            $password = "TIP2018_WebEngineering";
            $database = "tip";

            // Open the database
            $this->db = mysqli_connect("localhost", $username, $password);        
            if ($this->db == false) {
                die("Unable to connect to database");
            }

            // Select database
            mysqli_select_db($this->db, $database);
        }

       /**
         * Evaluates current time and adds a new guestbook entry with given name, 
         * e-Mail and comment.
         * @param String $name    User name
         * @param String $eMail   User e-mail address
         * @param String $comment The entry text
         * @return On success: Integer Index generated by the database for the entry
         *         On failure: Boolean false
         */
        public function addEntry($name, $eMail, $comment)
        {   
            // For security: suppress SQL injection
            $name    = mysqli_real_escape_string($this->db, $name);
            $eMail   = mysqli_real_escape_string($this->db, $eMail);
            $comment = mysqli_real_escape_string($this->db, $comment);

            // Add entry to the database
            $result = mysqli_query($this->db, "INSERT INTO guestbook (name, email, comment) VALUES ('$name', '$eMail', '$comment')");

            if ($result)
            {
                $result = mysqli_insert_id($this->db);
            }

            return $result;
        }

       /**
         * Return in an table (two-dimensional array) all entries of the guest book.
         * Each row of the table represents one entry in the guest book.
         * @return table[...]["Index"]   --> Integer: Index of the entry (for deleting)
         *         table[...]["Name"]    --> String: name of the user
         *         table[...]["eMail"]   --> String: e-Mail of the user
         *         table[...]["Comment"] --> String: The guest book entry (as text)
         *         table[...]["Date"]    --> String: Date and time of the entry
         */

    public function getEntries()
        {
            // Create query
            $result = mysqli_query($this->db, "SELECT * FROM guestbook");

            $table = false;
            $i = 0;
            while ($row = mysqli_fetch_array($result)) {
                $table[$i]["Index"]   = $row["indes"];
                $table[$i]["Date"]    = $row["date"];
                $table[$i]["Name"]    = $row["name"];
                $table[$i]["eMail"]   = $row["email"];
                $table[$i]["Comment"] = $row["comment"];
                $i++;
            }

            mysqli_free_result($result);

            return $table;

         // Get all entries in the guestbook
     $table = $guestbook->getEntries();

    if ($table) { // Check if there are enrtries
        echo "\nThe guestbook contains:\n";
        foreach ($table as $row) {
            // Output each element
            $index = $row["Index"];
            $name = $row["Name"];
            $date = $row["Date"];
            $email = $row["eMail"];
            $comment = $row["Comment"];

            echo "Index: $index, ";
            echo "Name: $name, ";
            echo "Date: $date, ";
            echo "eMail: $email, ";
            echo "Comment: $comment\n";
        }
    } 
    else {
        echo "\nGuest book is empty\n";

        }


        /**
         * Closes the database.
         */
         function __destruct()
        {
            mysqli_close($this->db);
        }

    }


    }
    ?>

最佳答案

您应该在输入标记中添加名称作为提交类型的 submit ,以便使 isset($_POST['submit']) 在 php 中工作

<input value="Send" type="submit" name="submit">

关于php - 表单数据未插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53831153/

相关文章:

MYSQL 在插入查询中使用多个选择返回列数与第 1 行的值数不匹配

mysql - 无法使用临时表添加或更新子行错误?

php - Mysql从多个表中选择,每个月创建一个新表

php - 优化查询——急切加载占用太多内存

php - PHP 的密码恢复邮件不起作用

mysql-workbench - 错误内置 :list-members on trying auto complete in mysql workbench

mysql - MySQL如何快速检索出行数最多的表?

PHP 生成表中的 Javascript 函数仅适用于第 1 行

php - 标记为崩溃的 mysql 表

php - 在 mysql 中进行平局排名并跳过不满足某个条件的某些行