Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more: , +
hello everyone. please help me.i am beginner i am getting 2 error.
It is true that similar problem have occurred and answers have published to them. I tried those and still I can't understand where my mistake is.
A help to catch my mistake would be really admired
Thank you


Notice: Undefined variable: query in C:\laragon\www\php\project7\admin\post.php on line 50

Warning: mysqli_query(): Empty query in C:\laragon\www\php\project7\admin\post.php on line 50
die






Here is my code:-


PHP
<pre><?php include "header.php"; ?>
  <div id="admin-content">
      <div class="container">
          <div class="row">
              <div class="col-md-10">
                  <h1 class="admin-heading">All Posts</h1>
              </div>
              <div class="col-md-2">
                  <a class="add-new" href="add-post.php">add post</a>
              </div>
              <div class="col-md-12">



<?php 
    if($_SESSION['user_role']=='0'){
      header("location: post.php");
    }
    include "config.php";
    
    
    #pagination
    $limit=3;
    if(isset($_GET['page'])){
    $page_number=$_GET['page'];
    }else{
    $page_number=1;
    }
    
    $offset=($page_number-1) * $limit;
   
    if($_SESSION['user_role']=='1'){
    
    $query="SELECT post.post_id, post.title, post.description, post.post_date, category.category_name,user.username FROM post 
    LEFT JOIN category ON post.category = category.category_id 
    LEFT JOIN user ON post.author = user.user_id
    ORDER BY post.post_id DESC LIMIT {$offset},{$limit}";
   
    }elseif ($_SESSION['user_role']=='0')
       {
        $query ="SELECT post.post_id, post.title, post.description, post.post_date, category.category_name, user.username FROM post 
        LEFT JOIN category ON post.category = category.category_id 
        LEFT JOIN user ON post.author = user.user_id
        WHERE post.author = {$_SESSION['user_id']}
        ORDER BY post.post_id DESC LIMIT {$offset},{$limit} ";
          
       }
      
     #data show and pagination
     $result= mysqli_query($connection,$query) or die("die");
    //  $posts = mysqli_fetch_all($result, MYSQLI_ASSOC);
   
    $count=mysqli_num_rows($result);
    if($count>0){

?>

                  <table class="content-table">
                      <thead>
                          <th>S.No.</th>
                          <th>Title</th>
                          <th>Category</th>
                          <th>Date</th>
                          <th>Author</th>
                          <th>Edit</th>
                          <th>Delete</th>
                      </thead>
                      <tbody>
    <?php 
    $serial_number=1; 
    while($row=mysqli_fetch_assoc($result)){
    ?>        
                          <tr>
                              <td class='id'><?php echo $serial_number++?></td>
                              <td><?php echo $row['title']?></td>
                              <td><?php echo $row['category_name']?></td>
                              <td><?php echo $row['post_date']?></td>
                              <td><?php echo $row['username']?></td>
                              <td class='edit'><a href='update-post.php?id=<?php echo $row['post_id']?>'></a></td>
                              <td class='delete'><a href='delete-post.php?id=<?php echo $row['post_id']?>'></a></td>
                          </tr>
 <?php } ?>             
                      </tbody>
<?php } ?>
                  </table>

                 

<!-- ----------------pagination --------------               -->

<?php
#-----------------------------------pagination---------
  include "config.php";
  $query2="SELECT * FROM post"; 
  $result2=mysqli_query($connection,$query2)  or die("Failed");
  if(mysqli_num_rows($result2)) {
      $total_records=mysqli_num_rows($result2);
      $total_page=ceil($total_records/$limit);

      echo "<ul class='pagination admin-pagination'>";

      if($page_number>1){
        echo '<li><a href="users.php?page='.($page_number-1).'">prev</a></li>';
      }
      
      for($i=1; $i<=$total_page; $i++){

        if($i==$page_number){
            $active="active";
        }else{
            $active="";
        }

        echo '<li class='.$active.'><a href="post.php?page='.$i.'">'.$i.'</a></li>';

      }

      if($total_page>$page_number){
        echo '<li><a href="post.php?page='.($page_number+1).'">Next</a></li>';
      }
    echo "</ul>";
  }  



?>
                 
                 
                 
                 
                 
                 
                 
                  <!-- <ul class='pagination admin-pagination'>
                      <li class="active"><a>1</a></li>
                      <li><a>2</a></li>
                      <li><a>3</a></li>
                  </ul> -->
              </div>
          </div>
      </div>
  </div>
<?php include "footer.php"; ?>

please help me where are error.

What I have tried:

$result= mysqli_query($connection,$query) or die("die");
Posted
Updated 25-Jun-21 17:43pm
v2

1 solution

What happens to $query when $_SESSION['user_role'] is neither '1' or '0' ?
PHP
if($_SESSION['user_role']=='1'){

$query="SELECT post.post_id, post.title, post.description, post.post_date, category.category_name,user.username FROM post
LEFT JOIN category ON post.category = category.category_id
LEFT JOIN user ON post.author = user.user_id
ORDER BY post.post_id DESC LIMIT {$offset},{$limit}";

}elseif ($_SESSION['user_role']=='0')
   {
    $query ="SELECT post.post_id, post.title, post.description, post.post_date, category.category_name, user.username FROM post
    LEFT JOIN category ON post.category = category.category_id
    LEFT JOIN user ON post.author = user.user_id
    WHERE post.author = {$_SESSION['user_id']}
    ORDER BY post.post_id DESC LIMIT {$offset},{$limit} ";

   }

 #data show and pagination
 $result= mysqli_query($connection,$query) or die(mysqli_error($result));
 
Share this answer
 
Comments
tanim arefin 25-Jun-21 23:50pm    
$_SESSION['user_role'] = '1' is Admin

and $_SESSION['user_role'] ='0'is moderator
Richard Deeming 29-Jun-21 6:36am    
Read Patrice's answer again.

What will $query contain if the session variable is not set, or contains something other than 1 or 0?

Now you need to debug your code to find out why the session variable doesn't contain what you expect.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900