【问题标题】:Undefined: index keywords (CMS)未定义:索引关键字(CMS)
【发布时间】:2015-02-01 00:23:32
【问题描述】:

我想在 PHP 和 MySQL 中制作一个 CMS,但我收到错误未定义:索引关键字,我在做什么错?

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

    }

}

}

【问题讨论】:

  • 请向我们展示您的 html 表单。这也意味着$_POST['keywords'] 没有设置!
  • 检查您的表单元素是否包含名称属性。如果没有,那就有问题了。
  • 旁注:'$post_keywords', $post_content - $post_content 代表一个字符串;引用它。

标签: php mysql database content-management-system


【解决方案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"

【讨论】:

  • "if (isset($_POST['title'])" - 未定义的不是“标题”,而是“关键字”。你还打错了“titte”。加上一个缺少的括号。 ^
  • 感谢提醒,两个键的概念都是一样的,但我明白你的意思
  • 重新加载我的评论,你错过了一些东西 ;-)
  • 好眼光!谢谢弗雷德
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    我以一种简单的方式做到了这一点:

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

    【讨论】:

      【解决方案4】:

      我以一种简单的方式做到了这一点:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        • 2020-09-15
        • 2015-07-25
        • 2012-12-08
        • 2012-09-18
        • 1970-01-01
        • 2013-06-15
        相关资源
        最近更新 更多