<分区>
我正在尝试创建一个简单的博客条目表单,用户可以在其中输入标题、博客条目并提交。然后,该表单应使用插入查询将“博客条目”插入到 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 的新手,我不知道如何在没有错误的情况下调试问题!
更新 1. 该解决方案奏效了。谢谢。但是我收到以下错误:
Notice: Undefined variable: error in C:\XAMPP\htdocs\blogwebsite\blog.php on line 30
我知道这是因为我还没有初始化 $error[] 数组。但是摆脱这个错误的标准方法是什么?请帮忙!