PHP - 未识别的索引变量[重复]

问题描述 投票:-2回答:1

Notice: Undefined index: categoryID in C:\xampp\htdocs\mit\postCategory.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>
php post get php-mysqlidb
1个回答
0
投票

您需要在$_GET中包含action变量:

<form method="post" action="postCategory.php?categoryID=1" enctype="multipart/form-data">

或者将其添加为隐藏文本字段并将其引用为$_POST变量。

© www.soinside.com 2019 - 2024. All rights reserved.