我想創建一個簡單的博客條目表單,用戶輸入標題,博客條目並提交它。然後,表單應該使用插入查詢將「博客條目」插入到MYSQL中。簡單的PHP'博客條目'表單不提交到MYSQL數據庫?
- 我得到沒有錯誤。
- 當我提交表單時,沒有任何更改,數據庫沒有新的 條目,並且表單不顯示「提交後」或「發佈未提交 」。
這裏是blog.php的代碼
<?php
// 1. Establish a connection using three functions: 1. mysqli_connect() 2. mysqli_connect_errno() 3. mysqli_connect_error()
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "blog";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Test if connection occured
if(mysqli_connect_errno()) {
die("Database connection failed " . mysqli_connect_error() . "(" . mysqli_connect_errno() . ")");
}
//Form submitted
if(isset($_POST['submit'])) {
//Error checking
if(!$_POST['title']) {
$error['title_error'] = "<p>Please supply a title.</p>\n";
}
if(!$_POST['blog']) {
$error['blog_error'] = "<p>Please supply blog content.</p>\n";
}
//No errors, process
if(!is_array($error)) {
//Process your form
// 2. Perform Your Query
$post_title = $POST["title"];
$post_content = $POST["blog"];
$query = "INSERT INTO entries (blog_id, blog_title, blog_content)
VALUES ('null', '{$post_title}', '{$post_content}')";
$result = mysqli_query($connection, $query);
//Display confirmation of post.
if($result) {
echo "Post submitted!";
} else {
echo "Error, post NOT submitted!";
}
//Require or include any page footer you might have
//here as well so the style of your page isn't broken.
//Then exit the script.
exit;
} else {
echo $error;
}
}
?>
<doctype>
<html>
<head>
<title> Blog </title>
</head>
<body>
<form method="POST" action="blog.php">
Title: <input name="title" type="text"> <br />
Blog: <textarea name="blog" cols="100" rows="5"> Blog Text here... </textarea> <br />
<input value="submit" type="submit" value="Submit" />
</form>
</body>
</html>
這裏是形式的屏幕拍攝後提交。
這裏是叫blog
MYSQL數據庫的截圖,和表名爲entries
:
有人知道我做錯了什麼。我是PHP的新手,我不知道如何在沒有錯誤的情況下調試問題!
UPDATE 1. 解決方案奏效。謝謝。不過,我收到以下錯誤:
Notice: Undefined variable: error in C:\XAMPP\htdocs\blogwebsite\blog.php on line 30
我知道這是因爲我還沒有初始化$錯誤[]數組。但是擺脫這個錯誤的標準方式是什麼?請幫忙!
它應該是'$ post_title = $ _POST [「title」]; $ post_content = $ _POST [「blog」];' –