我的代码的哪一部分导致了未定义索引错误? (PHP)

What part of my code is causing an Undefined Index error? (PHP)

我在一个小型论坛上工作,我目前正试图在一个单独的文件中显示论坛主题。无论出于何种原因,当我打开文件时它显示这些错误:

Notice: Undefined index: cid in C:\xampp\htdocs(A)Book 2.0\Bootstrap\topics.php on line 19

Notice: Undefined index: scid in C:\xampp\htdocs(A)Book 2.0\Bootstrap\topics.php on line 20

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs(A)Book 2.0\Bootstrap\content_function.php on line 41

我试过对变量使用 isset 函数,但没有用。我从一个单独的函数文件中调用变量。

这是显示主题的代码:

    <?php
session_start();
require_once ('../db.php');

// Check if user is logged in using the session variable
if ( $_SESSION['logged_in'] != 1 ) {
  $_SESSION['message'] = "You must log in before viewing your profile page!";
  header("location: error.php");
  exit; //<--- add this
}else {
    // Makes it easier to read
    $first_name = $_SESSION['first_name'];
    $last_name = $_SESSION['last_name'];
    $email = $_SESSION['email'];
    $active = $_SESSION['active'];
}

include ('content_function.php');
$cid2 = $_GET['cid'];
$scid2 = $_GET['scid'];
 ?>
<!DOCTYPE html>
<html lang = "en">
<head>
  <title>Topics</title>
  <link href="main.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div class = "content">
  <?php
  /*
  $select = mysqli_query($mysqli, "SELECT * FROM categories");
  while($row = mysqli_fetch_assoc($select)){
    echo  "<h1>", $row['category_title']."</h1>" ;
  }*/

  disptopics($cid2, $scid2, $mysqli);
  /*if (isset($_GET['cid'], $GET['scid'])){

    disptopics();
  }*/

   ?>
 </div>
</body>
</html>

另外,这里是函数的代码:

    <?php
require '../db.php';
  function dispcategories($mysqli){
    $select = mysqli_query($mysqli, "SELECT * FROM categories");

    while($row = mysqli_fetch_assoc($select)){
      echo "<table class = 'category-table'";
      echo "<tr><td class= 'main-category'colspan='2'>".$row['category_title']."</tr></td>";
      dispsubcategories($row['cat_id'], $mysqli);
      echo "</table>";
    }
  }

  function dispsubcategories($parent_id, $mysqli){
    $select = mysqli_query($mysqli, "SELECT cat_id, subcat_id, subcategory_title, subcategory_descr FROM categories, subcategories
                                     WHERE ($parent_id = categories.cat_id) AND ($parent_id = subcategories.parent_id)");
    echo "<tr><th width='90%'>Categories</th><th width='10%'>Topics</th></tr>";

    while($row = mysqli_fetch_assoc($select)){
      echo "<tr><td class='category_title'><a href='topics.php/".$row['cat_id']."/".$row['subcat_id']."'>
                    ".$row['subcategory_title']."<br/>";
      echo $row['subcategory_descr']."</a></td>";
      echo "<td class='num-topics'>".getnumtopics($parent_id, $row['subcat_id'], $mysqli)."</td></tr>";
    }
  }
  //Displays categories
  function getnumtopics($cat_id, $subcat_id, $mysqli){
    $select = mysqli_query($mysqli, "SELECT category_id, subcategory_id FROM topics WHERE ".$cat_id." = category_id
                    AND ".$subcat_id." = subcategory_id");
$get = mysqli_num_rows($select);

return $get;
  }
  //Displays Topics Within categories
  function disptopics($cid, $scid, $mysqli){
    $select = mysqli_query($mysqli, "SELECT topic_id, author, title, date_posted, views, replies FROM categories, subcategories, topics
                                     WHERE ($cid = topics.category_id) AND ($scid = topics.subcategory_id) AND ($cid = categories.cat_id)
                                     AND ($scid = subcategories.subcat_id) ORDER BY topic_id DESC");


   if(mysqli_num_rows($select) != 0) {
      echo "<table class='topic-table'>";
      echo "<tr><th>Title</th><th>Posted By</th><th>Date Posted</th><th>Views</th><th>Replies</th></tr>";

      while($row = mysqli_fetch_assoc($select)){
        echo "<tr><td><a href='readtopic.php/".$cid."/".$scid."/".$row['topic_id']."'>
              ".$row['title']."</a></td><td>".$row['author']."</td><td>".$row['date-posted'].".</td><td>".$row['views']."</td>
              <td>".$row['replies']."</td></tr>";
      }
      echo "</table>";
    } else {
      echo "<p>This category has no topics yet! <a href='newtopic.php/".$cid."/".$scid."'> Add a new one!</a></p>";
    }

  }


?>

我已多次查看每个文件并使用代码检查器显示我没有语法错误。

$_GET是一个数组,如果用户在查询字符串中没有定义scid=something,那么$_GET['scid']就是undefined。那里,你有一个未定义的索引通知。

在尝试读取它的值之前,您应该测试是否 isset($_GET['scid'])

$_GET['cid'] 和 $_GET['scid'] 从哪里获取数据请检查 echo $_GET['cid'] 或者你也做 var_dump($_GET);