php - 我花了数周的时间试图修复我的PHP代码。我需要一些指导

标签 php error-handling

我的代码假设要显示多部电​​影。但是,它仅显示一部电影。我知道我想告诉你我尝试过的事情。我已经移动了代码,检查了数据库,但是什么都没有改变。该代码曾经可以工作,但是我添加了一些新代码,现在这些代码不起作用。然后,我删除了新代码,但问题仍然存在。我有一个很多的代码,意识到这个问题将被删除。 ,这就是我要问的:如果您看到怪异的东西,请对此发表评论。

注意:我保证数据库信息没有背景,并且数据库中有多行信息。谢谢!

films.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>

更新

要注意的一件事是,一部电影是来自数据库中LAST行的结果,而不是数据库中FIRST行的结果。通常,仅在while循环出现问题时,信息才来自FIRST数据库。如果一个电影是来自数据库的最后一行,那意味着什么?我正在尝试提供更多信息,以便用户可以更好地了解如何解决此问题...

我提供了我的项目的图片:

Visual Aid of my project

最佳答案

这些是需要改进的地方

  • 始终将session_start()作为代码中的第一件事,这些包含的内容稍后可能会使用 session ,并且您的代码将失败– Muhammed M.刚才编辑
  • $name = $_GET["name"]; $name2 = $_GET["name2"];确保已设置它们,请替换为:$name=isset($_GET['name'])? $_GET['name']:'';如果设置了$ _GET ['name'],则它将包含在$ name中,否则$ name将为空字符串”。对所有$ _GET,$ _ POST或您只是假设它存在的其他变量执行此操作,而实际上可能并不存在。
  • 尝试更严格地将HTML与PHP分开。至少您可以做的是将所有与PHP相关的代码放在最上面,即使它的foreach可以构造表,将其分配给变量并包含在html中。最好有两个文件:main.phpmain_html.php,就像这样。
  • 在echo()之后有header(),它将无法正常工作,并且会出错。必须在输出之前调用header()。将所有if(..) { header()}移至页面顶部。重定向逻辑应该是代码中的第一个。

  • 还有其他事情,但是我觉得这些对于您而言是最关键的。

    还有一件事-请查看其他人的代码,学习如何正确编写代码,您将因此而喜欢PHP。不要看一些垃圾代码,不要怪PHP。例如下载著名的PHP框架,深入了解PSR-0,PSR-1等。这些都将帮助您成为更好的编码器。是的-不要放弃!许多年来,PHP一直在我背后,但我仍在每天学习))

    关于php - 我花了数周的时间试图修复我的PHP代码。我需要一些指导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35822392/

    相关文章:

    python - “finally”有多深?

    php - mysql - 从父表插入id

    php - file_get_contents 和 twitter 的奇怪问题

    php - 导入的 CSV 缺少字段

    iphone - EXC_BAD_ACCESS - 我怎样才能避免它?

    javascript - ASP.NET Ajax-OnFailure-获取错误文本

    javascript - 在基于 Ajax 的表单提交上显示不同的消息

    php - 设计/架构问题 : rollbacks with remote services

    node.js - 如何捕获 node.js aws-sdk 函数中引发的错误

    c# - 如何从 HttpResponseMessage(HttpStatusCode, Exception) 检索传递的异常