简单PHP';博客条目';表单未提交到MYSQL数据库?
我试图创建一个简单的博客条目表单,用户在其中输入标题、博客条目并提交。然后表单应该使用insert查询将“博客条目”插入MYSQL简单PHP';博客条目';表单未提交到MYSQL数据库?,php,html,mysql,forms,blogs,Php,Html,Mysql,Forms,Blogs,我试图创建一个简单的博客条目表单,用户在其中输入标题、博客条目并提交。然后表单应该使用insert查询将“博客条目”插入MYSQL 我没有错 当我提交表单时,没有任何更改,数据库没有新的 输入,并且表单不显示“已提交帖子”或“未提交帖子” 提交” 这是blog.php代码 <?php // 1. Establish a connection using three functions: 1. mysqli_connect() 2. mysqli_connect_errno()
- 我没有错李>
- 当我提交表单时,没有任何更改,数据库没有新的 输入,并且表单不显示“已提交帖子”或“未提交帖子” 提交”
<?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>
在html表单中,submit元素有两个“value”属性,但没有“name”属性。因此,在if(isset($\u POST['submit'])检查中找不到它,并且原始表单显示为没有发布任何内容。您的
if(isset($\u POST['submit']){
条件将不会设置为true,因为您没有为提交按钮设置名称标记
应该是
<input value="Submit" type="submit" name="submit"/>
但是,您仍然应该将mysqli.*
与mysql.*
API的函数一起使用,而不是使用mysqli.*
关于未定义变量
的错误,为什么不干脆放弃代码中的输入检查部分,并用以下内容替换它:
if(!empty($_POST["title"]) || !empty($_POST["blog"])){ /* IF BOTH INPUTS HAVE CONTENTS */
/* THE CODE YOU WANT TO EXECUTE */
}
else {
$error = "<p>Please supply a title or a blog content.</p>\n";
}
因此,您的PHP代码将是直截了当的:
if(isset($_POST["submit"])){
/* YOUR INSERT QUERY HERE */
}
required
,以及!empty()
条件的缺点是它接受空空间(
)。要绕过这一点,您应该使用支持此类内容的Javascript或库。它应该是$post\u title=$\u post[“title”];$post\u content=$\u post[“blog”]
解决方案成功了。谢谢。但是我遇到了以下错误:>注意:未定义变量:C:\XAMPP\htdocs\blogwebsite\blog.php第30行的错误我知道这是因为我没有初始化$error[]数组。但消除此错误的标准方法是什么?请帮助!感谢您的帮助!处理表单中错误的标准方法是什么,以及最理想的方法是什么?在您列出的选项中,最常用的方法是通过JavaScript吗?是的,JavaScript是彻底检查表单的方法。顺便说一句,如果我的答案确实对您有帮助,您可以投票或接受我的正确答案。@user3289740您可以发布一个新问题或搜索它,关于如何不接受空格。这是一个好技巧。谢谢。我想我应该开始与PHP一起学习JavaScript。
<input name="title" type="text" required>
<textarea name="blog" cols="100" rows="5" required>
if(isset($_POST["submit"])){
/* YOUR INSERT QUERY HERE */
}