Php 未定义:索引关键字(CMS)
我想在PHP和MySQL中创建一个CMS,但是我得到了一个未定义的错误:索引关键字,我做错了什么Php 未定义:索引关键字(CMS),php,mysql,database,content-management-system,Php,Mysql,Database,Content Management System,我想在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']
<?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>";
}
}
}
在访问$\u POST
数组之前,您需要验证该数组是否确实包含具有键关键字
的元素:
$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
}
我以一种简单的方式做到了这一点:
//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();
}
//保存在变量中的图像数据
echo$post\u image=isset($\u文件['post\u image']['name'])$_文件['post_image']['name']:'';
echo$image\u tmp=isset($\u文件['post\u image']['tmp\u name'])$_文件['post_image']['tmp_name']:'';
如果($post\u title==''或$post\u author==''){
回显“警报('请填写所有数据')”;
退出();
}
我用一种简单的方法完成了这项工作:
//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();
}
//将图像数据保存在变量中
echo$post\u image=isset($\u文件['post\u image']['name'])?
$\u文件['post\u image']['name']:'';
echo$image\u tmp=isset($\u文件['post\u image']['tmp\u name'])?
$\u文件['post\u image']['tmp\u name']:'';
如果($post\u title==''或$post\u author==''){
回显“警报('请填写所有数据')”;
退出();
}
请向我们展示您的html表单。也就是说,$\u POST['keywords']
没有设置!检查表单元素是否具有name属性。如果没有,那么就有问题了。旁注:“$post\u关键字”,$post\u content
-$post\u content
代表一个字符串;引用它。“if(isset($\u POST['title'))
”-没有定义的不是“title”,而是“keywords”。你还键入了一个“titte”。加上一个缺少的括号。^谢谢提醒,这两个键的概念都是一样的,但我明白你的意思。重新加载我的评论,你错过了一些东西;-)好眼力!谢谢你,弗雷德