php表单数据不会插入到数据库中

q3aa0525  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(431)

当我从表单提交数据时,它不会将表单数据插入数据库。数据库的连接已设置,当我检查连接时,它总是响应已连接。数据库正确设置了所有值:id、name、date、emailaddress和text。用户名、密码和数据库名称正确。
语法应该正确。我使用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);
        }

    }

    }
    ?>
s4n0splo

s4n0splo1#

你应该加上名字 submit 对于submit,请键入您的输入,以便 isset($_POST['submit']) 使用php

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

相关问题