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

标签 php mysqli forum

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

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 index 通知。

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

关于php - 我的代码的哪一部分导致了未定义索引错误? (PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51924069/

相关文章:

php - 根据条件参数过滤 WordPress 中的帖子

php - 哪种类型的输入最不容易受到攻击?

php - 如何在 Symfony2 中重新启动 session

php - Wordpress:基于插件的自定义帖子类型

php - 这是什么类型的时间戳,我需要将其转换为人类可读的时间戳

html - 将论坛软件(phpBB、MyBB 等)嵌入到静态网站框架(Jekyll、Middleman 等)中

php - 在论坛中实现帖子阅读标志的最佳方式?

php - 我可以在 PHP 中混合使用 MySQL API 吗?

PHP7 - MySQLI 将站点移动到新服务器后的错误处理问题

php - MySQLi 更新查询问题