首页  >  问答  >  正文

如何将生成的按钮的值从一个 PHP 文件传递​​到另一个 PHP 文件?

我想将 PHP 生成的按钮的 id 值从 theory.php 文件传递​​到 theory1.php 文件。 代码如下:

//theory.php file
require('components/db.php');

$query = "SELECT * FROM `courses`";
$result   = mysqli_query($connect, $query) or die("Error:" . mysqli_error($connect));;
$numrows = mysqli_num_rows($result);

for ($i = 0; $i < $numrows; $i++) {

    $query = "SELECT * FROM `courses` WHERE courseID = '$i'";
    $result   = mysqli_query($connect, $query) or die("Error:" . mysqli_error($connect));;
    $rowQuery = mysqli_fetch_assoc($result);

    $_SESSION['course_ID'] = $i;

    echo '
    <div class="card">
            <img class = "cardImage" src="';
    echo $rowQuery['imageLink'];
    echo '" alt="Course 1">
            <h3>';
    echo $rowQuery['courseName'];
    echo '</h3>
            <p>';
    echo $rowQuery['courseTextOne'];
    echo '</p>
            <a href="theory1.php?course_ID=$i" class="button">Proceed</a>
        </div>'; //a - is a button which needs to have an ID to pass to theory1.php
}

该代码生成带有按钮的卡片。我希望每个按钮都存储 MySQL 数据库中课程的相应 ID。该 ID 需要根据单击的按钮(卡)传递到另一个页面,以便将来可以从数据库中检索正确的数据。

P粉155832941P粉155832941424 天前550

全部回复(1)我来回复

  • P粉043432210

    P粉0434322102023-09-15 09:16:36

    解决方案

    理论.php:

    require('components/db.php');
    $query = "SELECT * FROM `courses`";
    $result   = mysqli_query($connect, $query) or die("Error:" . mysqli_error($connect));;
    $numrows = mysqli_num_rows($result);
    
    for ($i = 0; $i < $numrows; $i++) {
    
        $query = "SELECT * FROM `courses` WHERE courseID = '$i'";
        $result   = mysqli_query($connect, $query) or die("Error:" . mysqli_error($connect));;
        $rowQuery = mysqli_fetch_assoc($result);
    
        $_SESSION['course_ID'] = $i;
    
        echo '
        <div class="card">
                <img class = "cardImage" src="';
        echo $rowQuery['imageLink'];
        echo '" alt="Course 1">
                <h3>';
        echo $rowQuery['courseName'];
        echo '</h3>
                <p>';
        echo $rowQuery['courseTextOne'];
        echo '</p>
                <a href="theory1.php?courseID=';
        echo $i;
        echo '"class="button">Перейти</a>
            </div>'; //a - is a button which needs to have an ID to pass to theory1.php
    }

    theory1.php:

    <?php
    $courseID = $_GET['courseID'];
    echo $courseID;
    ?>

    回复
    0
  • 取消回复