PHP - 未识别的索引变量[重复]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP - 未识别的索引变量[重复]相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
Notice: Undefined index: categoryID in C:xampphtdocsmitpostCategory.php on line 14
我在下面的PHP代码中遇到此错误,我只通过会话获取categoryID并将其存储在$ categoryID的索引/变量中,然后在第20行调用此变量,我有$ query。我错过了什么?谢谢您的帮助!
<?php
session_start();
include("dbconnect.php");
if(!isset($_SESSION['admin'])) {
header("Location:index.php");
}
if(isset($_GET['categoryID'])) {
$_SESSION['postCategory']['categoryID']=$_GET['categoryID'];
}
if(isset($_POST['update']))
{
$categoryID=$_GET['categoryID'];
$postTitle = $_POST['postTitle'];
$postDesc = $_POST['postDesc'];
$postImage = $_FILES['image']['name'];
$target = "postImage/".basename($postImage);
$query = "UPDATE background SET postTitle = '$postTitle', postDesc = '$postDesc', postImage = '$postImage' WHERE post.categoryID ='$categoryID'";
$result = mysqli_query($con, $query);
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$msg = "Image uploaded successfully";
}else{
$msg = "Failed to upload image";
}
}
?>
<form method="post" action="postCategory.php" enctype="multipart/form-data">
<input type="text" name="postTitle" placeholder="Your Post Title">
<br>
<textarea name="postDesc" id="myTextarea">Your Post Description</textarea>
<br>
<input type="file" name="image">
<br>
<button type="submit" class="btn btn-primary" name="update">Post</button>
</form>
答案
您需要在$_GET
中包含action
变量:
<form method="post" action="postCategory.php?categoryID=1" enctype="multipart/form-data">
或者将其添加为隐藏文本字段并将其引用为$_POST
变量。
以上是关于PHP - 未识别的索引变量[重复]的主要内容,如果未能解决你的问题,请参考以下文章