Bagaimana untuk menghantar nilai butang yang dihasilkan dari satu fail PHP ke yang lain?
P粉155832941
P粉155832941 2023-09-14 21:00:35
0
1
576

Saya ingin menukar nilai id butang yang dijana oleh PHP daripada fail theory.php 文件传递​​到 theory1.php. Kodnya adalah seperti berikut:

//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
}

Kod ini menjana kad dengan butang. Saya mahu setiap butang menyimpan ID kursus yang sepadan dalam pangkalan data MySQL. ID ini perlu dihantar ke halaman lain berdasarkan butang (kad) yang diklik supaya data yang betul boleh diambil dari pangkalan data pada masa hadapan.

P粉155832941
P粉155832941

membalas semua(1)
P粉043432210

Penyelesaian

teori.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
}

teori1.php:

<?php
$courseID = $_GET['courseID'];
echo $courseID;
?>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan