如何将生成的按钮的值从一个 PHP 文件传递​​到另一个 PHP 文件?
P粉155832941
P粉155832941 2023-09-14 21:00:35
0
1
564

我想将 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粉155832941
P粉155832941

全部回复(1)
P粉043432210

解决方案

理论.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;
?>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板