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

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

当我从表单提交数据时,它不会将表单数据插入数据库。数据库的连接已设置,当我检查连接时,它总是响应已连接。数据库正确设置了所有值:id、name、date、emailaddress和text。用户名、密码和数据库名称正确。
语法应该正确。我使用mysql工作台。

  1. <html>
  2. <head>
  3. <meta http-equiv="content-type" content="text/html; charset=windows-1252">
  4. <title></title>
  5. </head>
  6. <body>
  7. <link rel="stylesheet" type="text/css" href="input.css">
  8. <header>
  9. <h1></h1>
  10. </header>
  11. <nav>
  12. <ul>
  13. <li></li>
  14. <li></li>
  15. <li></li>
  16. <li></li>
  17. <li></li>
  18. </ul>
  19. </nav>
  20. <main>
  21. <form method="post" action="input.php">
  22. <label>Name</label>
  23. <input placeholder="Name" name="name" type="text"><br>
  24. <label>Mailadress</label>
  25. <input placeholder="Emailadress" name="eMail" type="email"><br>
  26. <label>Your comment</label>
  27. <textarea name="comment" placeholder="Text" name="comment"cols="60" rows="15"></textarea>
  28. <input value="Send" type="submit">
  29. </form>
  30. </main>
  31. </body>
  32. </html>

php代码:

  1. <?php
  2. if($con->connect_error)
  3. {
  4. die("No connection" .$con-> connect_error);
  5. }
  6. echo "Connected";
  7. $guestbook = new GuestbookAccess();
  8. if (isset($_POST['submit'])){
  9. $name = $_POST['name']; $eMail = $_POST['eMail']; $comment = $_POST['comment'];
  10. }
  11. class GuestbookAccess
  12. {
  13. private $db;
  14. /**
  15. * Opens the database.
  16. */
  17. public function __construct()
  18. {
  19. $username = "tipuser";
  20. $password = "TIP2018_WebEngineering";
  21. $database = "tip";
  22. // Open the database
  23. $this->db = mysqli_connect("localhost", $username, $password);
  24. if ($this->db == false) {
  25. die("Unable to connect to database");
  26. }
  27. // Select database
  28. mysqli_select_db($this->db, $database);
  29. }
  30. /**
  31. * Evaluates current time and adds a new guestbook entry with given name,
  32. * e-Mail and comment.
  33. * @param String $name User name
  34. * @param String $eMail User e-mail address
  35. * @param String $comment The entry text
  36. * @return On success: Integer Index generated by the database for the entry
  37. * On failure: Boolean false
  38. */
  39. public function addEntry($name, $eMail, $comment)
  40. {
  41. // For security: suppress SQL injection
  42. $name = mysqli_real_escape_string($this->db, $name);
  43. $eMail = mysqli_real_escape_string($this->db, $eMail);
  44. $comment = mysqli_real_escape_string($this->db, $comment);
  45. // Add entry to the database
  46. $result = mysqli_query($this->db, "INSERT INTO guestbook (name, email, comment) VALUES ('$name', '$eMail', '$comment')");
  47. if ($result)
  48. {
  49. $result = mysqli_insert_id($this->db);
  50. }
  51. return $result;
  52. }
  53. /**
  54. * Return in an table (two-dimensional array) all entries of the guest book.
  55. * Each row of the table represents one entry in the guest book.
  56. * @return table[...]["Index"] --> Integer: Index of the entry (for deleting)
  57. * table[...]["Name"] --> String: name of the user
  58. * table[...]["eMail"] --> String: e-Mail of the user
  59. * table[...]["Comment"] --> String: The guest book entry (as text)
  60. * table[...]["Date"] --> String: Date and time of the entry
  61. */
  62. public function getEntries()
  63. {
  64. // Create query
  65. $result = mysqli_query($this->db, "SELECT * FROM guestbook");
  66. $table = false;
  67. $i = 0;
  68. while ($row = mysqli_fetch_array($result)) {
  69. $table[$i]["Index"] = $row["indes"];
  70. $table[$i]["Date"] = $row["date"];
  71. $table[$i]["Name"] = $row["name"];
  72. $table[$i]["eMail"] = $row["email"];
  73. $table[$i]["Comment"] = $row["comment"];
  74. $i++;
  75. }
  76. mysqli_free_result($result);
  77. return $table;
  78. // Get all entries in the guestbook
  79. $table = $guestbook->getEntries();
  80. if ($table) { // Check if there are enrtries
  81. echo "\nThe guestbook contains:\n";
  82. foreach ($table as $row) {
  83. // Output each element
  84. $index = $row["Index"];
  85. $name = $row["Name"];
  86. $date = $row["Date"];
  87. $email = $row["eMail"];
  88. $comment = $row["Comment"];
  89. echo "Index: $index, ";
  90. echo "Name: $name, ";
  91. echo "Date: $date, ";
  92. echo "eMail: $email, ";
  93. echo "Comment: $comment\n";
  94. }
  95. }
  96. else {
  97. echo "\nGuest book is empty\n";
  98. }
  99. /**
  100. * Closes the database.
  101. */
  102. function __destruct()
  103. {
  104. mysqli_close($this->db);
  105. }
  106. }
  107. }
  108. ?>
s4n0splo

s4n0splo1#

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

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

相关问题