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

时间:2016-03-06 02:29:00

标签: php error-handling

我的代码假设要显示多部电​​影。但是,它只显示一部电影。我知道我想告诉你我的尝试。我已经移动了代码,检查了数据库,但没有任何改变。用于工作的代码,但我添加了一些新代码,代码现在无法正常工作。然后我删除了新代码,但问题仍在继续。我有一个很多的代码,并意识到这个问题将被删除。 这就是我要问的:如果你看到一些奇怪的东西,请对它进行评论。

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

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>

更新

有一点需要注意的是,ONE电影来自数据库中最后一行的结果,而不是数据库中的第一行。通常,如果while循环出现问题,则信息仅来自FIRST数据库。如果ONE电影来自数据库中的最后一行,这意味着什么?我正在尝试提供更多信息,以便用户更好地了解如何解决此问题...

我提供了我的项目图片:

Visual Aid of my project

1 个答案:

答案 0 :(得分:1)

这些是一些需要改进的地方

  1. 总是把session_start()作为代码中的第一件事,后面的那些包括会使用会话而你的代码会失败 - Muhammed M.刚刚编辑

  2. $name = $_GET["name"]; $name2 = $_GET["name2"];确保它们已设置,如下所示替换:$name=isset($_GET['name'])? $_GET['name']:'';如果设置了$ _GET [&#39; name&#39;],您将以$ name命名,否则$ name将为空字符串&#39;&#39;。对所有$ _GET,$ _POST或任何其他变量执行此操作只是假设它就在那里,而实际上它可能不存在。

  3. 尝试更难从PHP中分离HTML。至少你可以做的就是把所有与PHP相关的代码放在首位,即使它的代码是&#39; foreach构造你的表,将它分配给一个变量,并包含在你的html中。最好有两个文件:main.phpmain_html.php,你得到它,就像这样。

  4. 你在echo()之后有header(),它不会工作,会给出错误。必须在任何输出之前调用header()。将您的所有if(..) { header()}移至页面顶部。该重定向逻辑应该是代码中的第一个。

  5. 还有其他一些事情,但我觉得这些对你来说至关重要。

    还有一件事 - 请查看其他代码,了解如何正确编码,并且您会喜欢PHP。不要看一些垃圾代码并指责PHP。下载众所周知的PHP框架,挖掘内部了解PSR-0,PSR-1等等。这些都将帮助您成为更好的程序员。是的 - 不要放弃!许多年的PHP背后,但我仍然每天都在学习))