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

时间:2018-08-20 04:14:15

标签: php mysqli forum

我正在一个小型论坛上工作,当前正在尝试将论坛主题显示在单独的文件中。无论出于什么原因,当我打开文件时,它都会显示以下错误:

  

注意:未定义索引:第19行C:\ xampp \ htdocs(A)Book 2.0 \ Bootstrap \ topics.php中的cid

     

注意:未定义索引:C:\ xampp \ htdocs(A)Book中的scid   第20行的2.0 \ Bootstrap \ topics.php

     

警告:mysqli_num_rows()期望参数1为mysqli_result,   C:\ xampp \ htdocs(A)Book中给出的布尔值   第41行的2.0 \ Bootstrap \ content_function.php

我试图为变量使用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>";
    }

  }


?>

我已经多次检查了每个文件,并使用了代码检查器,说我没有语法错误。

2 个答案:

答案 0 :(得分:2)

$_GET是一个数组,如果用户未在查询字符串中定义scid=something,则$_GET['scid']未定义。在那里,您得到了未定义的索引声明。

在尝试读取isset($_GET['scid'])的值之前,应该先进行测试。

答案 1 :(得分:0)

此$ _GET ['cid']和$ _GET ['scid']从何处获取数据,请检查echo $ _GET ['cid']或也执行var_dump($ _ GET);