Rumah  >  Soal Jawab  >  teks badan

Bagaimana untuk menghantar nilai butang yang dihasilkan dari satu fail PHP ke yang lain?

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粉155832941P粉155832941424 hari yang lalu551

membalas semua(1)saya akan balas

  • P粉043432210

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

    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;
    ?>

    balas
    0
  • Batalbalas