未定义:索引关键字 (CMS)


Undefined: index keywords (CMS)

我想用PHP和MySQL制作一个CMS,但是我得到错误Undefined:index关键字,我做错了什么?

<?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>";
    }
}
}

访问 $_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'] : '';

另一种编写方式是IE:

if (isset($_POST['keywords'])) {
    $post_keywords = $_POST['keywords'];
} else {
    $post_keywords = '';
}

如果您希望每次都发布关键字,则应验证输入字段的名称是否与 post 数组中的键完全匹配,并且没有拼写错误。

  • 还应确保窗体的元素包含 name 属性。

即:name="keywords"

您可以设置一个条件来验证是否有任何 POST 数据。

if ($_POST) {
   //do your staff here
} else {
   //redirect to the form
}

我以一种简单的方式完成了此操作:

//Save images data in variable
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();
}

我以一种简单的方式完成了此操作:

//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();
}