当我从表单提交数据时,它不会将表单数据插入数据库中。设置了与数据库的连接,当我检查连接时,它始终会响应已连接。使用所有值正确设置数据库: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);
}
}
}
?>
答案 0 :(得分:1)
您应该在输入年龄中将名称添加为submit
作为提交类型,以使isset($_POST['submit'])
在php中工作
<input value="Send" type="submit" name="submit">