我想在PHP和MySQL中创建一个CMS,但是我收到错误Undefined:index keywords,我做错了什么?
<?php
include("includes/connect.php");
if (isset($_POST['submit'])){
$post_title = $_POST['title'];
$post_date = date('d-m-y');
$post_author = $_POST['author'];
$post_keywords = $_POST['keywords'];
$post_content = $_POST['content'];
$post_image = $_FILES['image'] ['name'];
$image_tmp = $_FILES['image'] ['tmp_name'];
if($post_title=='' or $post_keywords=='' or $post_content=='' or $post_author==''){
echo "<script>alert('any of the field is empty')</script>";
exit();
}
else{
move_uploaded_file($image_tmp, "images/$post_image");
$insert_query = "insert into posts (post_title,post_date,post_author,post_image,post_keywords,post_content) values ('$post_title','$post_date','$post_author','$post_image', '$post_keywords', $post_content)";
if (mysql_query($insert_query)) {
echo "<center><h1>Post Published succesfully!</h1></center>";
}
}
}
答案 0 :(得分:1)
在访问$_POST
数组之前,您需要验证keywords
数组实际上是否包含键$post_title = isset($_POST['title']) ? $_POST['title'] : '';
$post_date = date('d-m-y');
$post_author = isset($_POST['author']) ? $_POST['author'] : '';
$post_keywords = isset($_POST['keywords']) ? $_POST['keywords'] : '';
$post_content = isset($_POST['content']) ? $_POST['content'] : '';
$post_image = isset($_FILES['image']['name']) ? $_FILES['image']['name'] : '';
$image_tmp = isset($_FILES['image']['tmp_name']) ? $_FILES['image']['tmp_name'] : '';
的元素:
if (isset($_POST['keywords'])) {
$post_keywords = $_POST['keywords'];
} else {
$post_keywords = '';
}
另一种写这种方式的方式是:
name="keywords"
如果您希望每次验证输入字段的名称与post数组中的键完全匹配且没有拼写错误时发布关键字。
即:{{1}}
答案 1 :(得分:0)
您可以设置一个条件来验证是否有任何POST数据。
if ($_POST) {
//do your staff here
} else {
//redirect to the form
}
答案 2 :(得分:0)
我这样做的方式很简单:
HttpContext.Response.ContentType = "application/pdf";
var result = new FileContentResult(System.IO.File.ReadAllBytes("THE PATH/test.pdf"), "application/pdf")
{
FileDownloadName = "test.pdf"
};
return result;
答案 3 :(得分:-1)
我这样做的方式很简单:
//Images data save in Variables
echo $post_image = isset($_FILES['post_image']['name']) ? $_FILES['post_image']['name'] : '';
echo $image_tmp = isset($_FILES['post_image']['tmp_name']) ? $_FILES['post_image']['tmp_name'] : '';
if($post_title=='' OR $post_author==''){
echo "<script>alert('Please fill all the data')</script>";
exit();
}