gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:56:00 28 4
gpt4 key购买 nike

当我从表单提交数据时,它不会将表单数据插入到数据库中。与数据库的连接已设置,当我检查连接时,它总是响应已连接。数据库已正确设置所有值: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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com