首页 > 解决方案 > 在 PHP 参数中调用表

问题描述

我知道这与参数有关,在我的 HTML 页面上,我正在尝试自动执行下一个按钮,所以假设我有一个系列的第一集、动漫或其他任何东西,所以首先所有剧集将来自我使用 id 作为参数创建的仪表板,所以它们都将以 example.com/episode.php 的形式出现?id=1 以使 HTML 接收我使用此调用的所有可替换变量

<?php
// Check existence of id parameter before processing further
if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
    // Include config file
    include_once "config.php";
    
    // Prepare a select statement
    $sql = "SELECT * FROM testEpisode WHERE id = ?";
    
    if($stmt = mysqli_prepare($conn, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "i", $param_id);
        
        // Set parameters
        $param_id = trim($_GET["id"]);
        
        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            $result = mysqli_stmt_get_result($stmt);
    
            if(mysqli_num_rows($result) == 1){
                /* Fetch result row as an associative array. Since the result set contains only one row, we don't need to use while loop */
                $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
                
                
                // Retrieve individual field value
                $name = $row["name"];
                $episode = $row["episode"];
                $video1 = $row["video1"];
                $video2 = $row["video2"];
            }
        } else{
            echo "Oops! Something went wrong. Please try again later.";
        }
    }
     
    // Close statement
    mysqli_stmt_close($stmt);
    
    // Close connection
    mysqli_close($conn);
} 

?>

这样就给出了带有我已经输入的所有变量的 URL 和剧集,但这是我想要创建下一个按钮的 HTML 上的某处,让我尝试重新创建它,因为我已经单独测试了它并且它可以工作,我创建下一个按钮的主要想法是

<?php
$sql = 'SELECT * FROM testEpisode ORDER by id desc limit 1';
if($result = mysqli_query($conn, $sql)){
 while($row = mysqli_fetch_array($result)) {
     $x = $row['id'];   
 }
}

主要思想是,如果此页面的 id 等于在表中输入的最后一个 id,则执行此

if($row['id'] == $x){
      $a=no;

}else{
      $a=yes;
      $b = $row['id'] + 1;
}

echo '<div class="' . $a . '">';       
        
echo '<a id="link"  href="read.php?id=' . $b . '">';

echo '<i class="fas fa-caret-right" style="font-size:30px;color:white"></i><p class="letter"> Next</p></a>';

echo '</div>';

似乎是因为我在我的 id 上获得的参数,我无法获得该表上的最后一个 id,我尝试了很多东西,它要么是那个 HTML 的主 id,要么是最后一个 id ,它不会让我打印两者,知道怎么做吗?

标签: phpmysqlsql

解决方案


推荐阅读