我花了几周时间尝试修复我的PHP代码。 我需要一些指导

my code is suppose to display multiple movies. However, it is only displaying ONE movie. I know I'm suppose to tell you what I've tried. I've moved code, checked the db, but nothing has changed. The code used to work, but I added some new code and the code is now not working. I then deleted the new code but the problem continued. I have a lot of code and realize this question will just be deleted. This is all I'm asking: If you see something weird, comment on it.

Note: I promise there is no background with the db information and that there is more than one row of information in the db. Thanks!

movies.php

<?php

error_reporting(0);

require "start.php";
require "dropbox_auth.php";

session_start();

if(!$_SESSION["id"]) {
  header("location: index.php");
}

$id2 = $_SESSION["id"];

$name = $_GET["name"];
$name2 = $_GET["name2"];

$db2 = new mysqli("127.0.0.1", "root", "", "crave crap");
$one2 = $db2->query("SELECT * FROM users WHERE id='$id2'");
$two2 = $one2->fetch_object();
$three2 = $two2->username;
$four2 = $two2->premium;

if(isset($name)) {
  $findName = $db2->query("SELECT * FROM movies WHERE id='$name'");
  $fetchName = $findName->fetch_object();
  $real_title = $fetchName->title;
  echo "<div id='notVerifiedDiv'> <span id='notVerifiedText'> <strong> $real_title </strong> added to liked movies </span> </div> </div>";
  $page = $_GET["page"];
  $sorting = $_GET["sorting"];
  header("Refresh:1; url=http://localhost/Drop%20Box/movies.php?page=".$page."&sorting=".$sorting);
}

if(isset($name2)) {
  $findName2 = $db2->query("SELECT * FROM movies WHERE id='$name2'");
  $fetchName2 = $findName2->fetch_object();
  $real_title2 = $fetchName2->title;
  echo "<div id='notVerifiedDiv'> <span id='notVerifiedText'> <strong> $real_title2 </strong> removed from liked movies </span> </div> </div>";
  $page = $_GET["page"];
  $sorting = $_GET["sorting"];
  header("Refresh:1; url=http://localhost/Drop%20Box/movies.php?page=".$page."&sorting=".$sorting);
}

if(!$four2) {
  // They are not premium
  header("location: premium.php");
}

?>

<!DOCTYPE html>
<html>
  <head>
    <title> Movies </title>
    <link rel="stylesheet" href="http://fontawesome.io/assets/font-awesome/css/font-awesome.css">
    <link rel='stylesheet' href='main.css'>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script src='main.js'></script>
  </head>

  <body>
    <!-- Navigation -->
      <div id='nav'>
         <!-- Profile Wrapper -->
      <ul class="profile-wrapper">
            <li>
                <!-- user profile -->
                <div class="profile">
                    <img src="avatars/default.png" />
                    <a href="#" class="name"> <?php echo $three2; ?> <i class="fa fa-caret-down" id="caret-down"></i></a>

                    <!-- more menu -->
                    <ul class="menu">
                      <a href="#"><li>Sign Out</li></a>
                        <a href="#"><li>Sign Out</li></a>
                        <a href="logout.php"><li>Sign Out</li></a>
                    </ul>
                </div>
            </li>
        </ul>
    <!-- End of Profile Wrapper -->
      </div>
    <!-- End of Navigation -->

    <!-- Movie Content -->
      <div id='movie_content2'>
        <div id='movie_line'>
          <div id='movie_line2'></div>

          <!-- Dropdown Selection -->

          <?php

          $sorting = $_GET["sorting"];
          $page = $_GET["page"];

          if($sorting == "") {
            header("location: http://localhost/Drop%20Box/movies.php?page=1&sorting=recent");
          }

          if($sorting == "recent" || $sorting !== "popular" && $sorting !== "history" && $sorting !== "likes") {
            echo '<nav>
          <ul id="dropdown_selection">
            <li><a href="#">Most Recent</a>
              <ul>
                <li><a href="http://localhost/Drop%20Box/movies.php?page=1&sorting=popular"> &nbsp; Most Liked </a></li>
                <li><a href="http://localhost/Drop%20Box/movies.php?page=1&sorting=history"> &nbsp; My History </a></li>
                <li><a href="http://localhost/Drop%20Box/movies.php?page=1&sorting=likes"> &nbsp; My Likes </a></li>
              </ul>
            </li>
          </ul>
        </nav>';
          }

          if($sorting == "popular") {
            echo '<nav>
          <ul id="dropdown_selection">
            <li><a href="#">Most Liked</a>
              <ul>
                <li><a href="http://localhost/Drop%20Box/movies.php?page=1&sorting=recent"> &nbsp; Most Recent </a></li>
                <li><a href="http://localhost/Drop%20Box/movies.php?page=1&sorting=history"> &nbsp; My History </a></li>
                <li><a href="http://localhost/Drop%20Box/movies.php?page=1&sorting=likes"> &nbsp; My Likes </a></li>
              </ul>
            </li>
          </ul>
        </nav>';
          }

          if($sorting == "history") {
             echo '<nav>
          <ul id="dropdown_selection">
            <li><a href="#">My History</a>
              <ul>
                <li><a href="http://localhost/Drop%20Box/movies.php?page=1&sorting=recent"> &nbsp; Most Recent </a></li>
                <li><a href="http://localhost/Drop%20Box/movies.php?page=1&sorting=popular"> &nbsp; Most Liked </a></li>
                <li><a href="http://localhost/Drop%20Box/movies.php?page=1&sorting=likes"> &nbsp; My Likes </a></li>
              </ul>
            </li>
          </ul>
        </nav>';
          }

          if($sorting == "likes") {
            echo '<nav>
          <ul id="dropdown_selection">
            <li><a href="#">My Likes</a>
              <ul>
                <li><a href="http://localhost/Drop%20Box/movies.php?page=1&sorting=recent"> &nbsp; Most Recent </a></li>
                <li><a href="http://localhost/Drop%20Box/movies.php?page=1&sorting=popular"> &nbsp; Most Liked </a></li>
                <li><a href="http://localhost/Drop%20Box/movies.php?page=1&sorting=history"> &nbsp; My History </a></li>
              </ul>
            </li>
          </ul>
        </nav>';
          }

          ?>

          <!-- End of Dropdown Selection -->

         <?php

    // Pagination

    $per_page = 10;
    $count_total = $db2->query("SELECT * FROM movies");
    $pages = $count_total->num_rows;
    $total_pages = ceil($pages / $per_page);

    if(!isset($_GET["page"])) {
      header("location: movies.php?page=1");
    } else {
      $page = $_GET["page"];
    }

    $start = (($page - 1) * $per_page);


    $sorting = $_GET["sorting"];

    if($sorting == "recent" || $sorting !== "popular" && $sorting !== "history" && $sorting !== "likes") {
    $movie = $db2->query("SELECT * FROM movies LIMIT $start, $per_page");
    }

    if($sorting == "popular") {
      $movie = $db2->query("SELECT * FROM movies ORDER BY likes DESC LIMIT $start, $per_page");
    }

    session_start();

    $id3 = $_SESSION["id"];

    if($sorting == "history") {
     $movie = $db2->query("SELECT m.id AS mid,m.photo AS pho, m.destination AS des,m.length AS len,m.length_content AS lenc,m.description AS desa,m.rating AS rat FROM movies m INNER JOIN history h ON h.movie_id = m.id WHERE h.user_id = $id3 ORDER BY h.id LIMIT $start, $per_page");
    }

    if($sorting == "likes") {
      $movie = $db2->query("SELECT * FROM movies m INNER JOIN likes h ON h.number_likes = m.id WHERE h.user = $id3 ORDER BY h.id LIMIT $start, $per_page");
    }



    $number = 0;

        $fetch_movie = $movie->num_rows;

        if($fetch_movie > 0) {

         while($movie3 = mysqli_fetch_array($movie)) {
           $number++;
           if($sorting !== "history") {
           $id2 = $movie3["id"];
           $photo = $movie3["photo"];
           $rating = $movie3["rating"];
           $destination = $movie3["destination"];
            $length = $movie3["length"];
            $length_content = $movie3["length_content"];

           $description = $movie3["description"];
           } else {
             $id2 = $movie3["mid"];
             $photo = $movie3["pho"];
             $destination = $movie3["des"];
             $length = $movie3["len"];
             $length_content = $movie3["lenc"];
             $description = $movie3["desa"];
             $rating = $movie3["rat"];
           }

           $sorting = $_GET["sorting"];

           $findLike = $db2->query("SELECT * FROM likes WHERE number_likes='$id2'");
          $numLike = $findLike->num_rows;

           echo "<div id='descriptive_div' number='$number'> <i class='fa fa-caret-left' id='descriptive_caret' number='$number'></i> <a href='like.php?number=$id2&page=$page'>";

           $sorting = $_GET["sorting"];

          if($sorting !== "likes") {
          if($numLike == 0) {
            // Not Liked
           echo "<a href='like.php?number=$id2&page=$page&code=1&sorting=$sorting'> <div class='like_button' number='$number'> Like </div> </a>";
          } else {
            echo "<a href='like.php?number=$id2&page=$page&sorting=$sorting'> <div class='liked_button' number='$number'> Like </div> </a>";
          }
          } else if($sorting == "likes") {
            echo "<a href='like.php?number=$id2&page=$page&sorting=$sorting'> <div class='liked_button' number='$number'> Like </div> </a>";
          }

           echo "</a> <span id='descriptive_div_text'> $description </span> </div>";

           if($length_content) {
            // hr & min
            echo "<a href='open.php?destination=$destination'> <div class='movie_length' number='$number'> <div id='movie_length_text' number='$number'> $length </div> </div> </a>";
           } else {
              // min
              echo "<a href='open.php?destination=$destination'> <div class='movie_length2' number='$number'> <span id='movie_length_text' number='$number'> $length </span> </div> </a>";
           }

           $sorting = $_GET["sorting"];

           if($rating == "PG-13") {
             if($sorting !== "likes") {
             if($numLike == 0) {
               // Not Liked
               echo "<a href='open.php?destination=$destination'> <div class='movie_rating' number='$number'> <span id='movie_rating_text' number='$number'> $rating </span> </div> </a>";
             } else {
               // Liked
               echo "<a href='open.php?destination=$destination'> <div class='movie_rating4' number='$number'> <span id='movie_rating_text' number='$number'> $rating </span> </div> </a>";
             }
             } else if($sorting == "likes") {
               // Liked
               echo "<a href='open.php?destination=$destination'> <div class='movie_rating4' number='$number'> <span id='movie_rating_text' number='$number'> $rating </span> </div> </a>";
             }
           }

           if($rating == "PG") {
             if($sorting !== "likes") {
             if($numLike == 0) {
               // Not Liked
             echo "<a href='open.php?destination=$destination'> <div class='movie_rating2' number='$number'> <span id='movie_rating_text' number='$number'> $rating </span> </div> </a>";
             } else {
               // Liked
               echo "<a href='open.php?destination=$destination'> <div class='movie_rating5' number='$number'> <span id='movie_rating_text' number='$number'> $rating </span> </div> </a>";
             }
             } else if($sorting == "likes") {
               // Liked
               echo "<a href='open.php?destination=$destination'> <div class='movie_rating5' number='$number'> <span id='movie_rating_text' number='$number'> $rating </span> </div> </a>";
             }
           }

           if($rating == "G" || $rating == "R") {
             if($sorting !== "likes") {
             if($numLike == 0) {
               // Not Liked
             echo "<a href='open.php?destination=$destination'> <div class='movie_rating3' number='$number'> <span id='movie_rating_text' number='$number'> $rating </span> </div> </a>";
             } else {
               // Liked
               echo "<a href='open.php?destination=$destination'> <div class='movie_rating6' number='$number'> <span id='movie_rating_text' number='$number'> $rating </span> </div> </a>";
             }
             } else if($sorting == "likes") {
               // Liked
               echo "<a href='open.php?destination=$destination'> <div class='movie_rating6' number='$number'> <span id='movie_rating_text' number='$number'> $rating </span> </div> </a>";
             }
           }

           }


           if($numLike) {
              // Liked
              echo "<a href='open.php?destination=$destination'> <img src='$photo' class='movie_size3' number='$number'> </a>";
            } else {
              // Not Liked
              echo "<a href='open.php?destination=$destination'> <img src='$photo' class='movie_size' number='$number'> </a>";
            }

        } else {
           echo "No Movies to Display";
        }

         if($fetch_movie !== 0) {
         echo "<br>";

         for($number=1;$number<=$total_pages;$number++) {
           if($page == $number) {
             echo '<div class="complete_page">'.$number.'</div>';
           } else {
             $sorting = $_GET["sorting"];
            echo '<a href="?page='.$number.'&sorting='.$sorting.'"> <div class="number_page">'.$number.'</div></a>';
           }
         }
         }

           ?>
        </div>
      </div>
    <!-- End of Movie Content -->
  </body>
</html>

UPDATE

One thing to note is that the ONE movie is from the results of the LAST row in the db and not the FIRST row in the db. Usually, the information only comes from the FIRST db if there is a problem with the while loop. What does it mean if the ONE movie is coming from the last row in the db? I'm trying to provide more info so users can have a better understanding of how to solve this problem...

I have provided an image of my project:

Visual Aid of my project

These are some spots for improvement

  1. always put session_start() as the first thing in code, those includes later may use sessions and your code will fail – Muhammed M. just now edit

  2. $name = $_GET["name"]; $name2 = $_GET["name2"]; make sure they are set, replace like this: $name=isset($_GET['name'])? $_GET['name']:''; if $_GET['name'] is set, you will have it in $name, otherwise $name will be empty string ''. Do this for all $_GET, $_POST, or any other variable you just assume it is there, when in fact it might not be.

  3. Try separating HTML from PHP harder. At least you could do is have all PHP related code on top, even if its' foreach that constructs your table, assign it in a variable, and include in you html. Better to have two files: main.php, main_html.php, you get it, something like this.

  4. You have header() after echo(), it won't work, will give error. header() must be called before any output. Move all of your if(..) { header()} to the top of the page. That redirection logic should be the first in your code.

There are other things, but I feel like these are the most critical at this point for you.

And one more thing - please look at other's code, learn how to code properly, and you will love PHP for that. Don't look at some junky code and blame PHP. Download well known PHP frameworks for instance, dig inside learn what PSR-0, PSR-1... etc are. These all will help you become a better coder. And yes - don't give up! Many many years of PHP behind my back, but I am still learning every day ))