-2

I'd like to pass an id value of a PHP generated button from theory.php file to theory1.php file. Here's the code:

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

The code generates cards with buttons. I want each button to store a respective ID from the course from MySQL database. That ID needs to be passed to another page depending on which button (card) is clicked, so it can retrieve the right data from the database in the future.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 2
    It is a very bad idea to use `die(mysqli_error($$conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Apr 05 '23 at 09:46
  • 2
    `$variables` are only expanded if they are inside a double quoted string literal. You are using a single quoted string literal around your paragraph and anchor tags – RiggsFolly Apr 05 '23 at 10:14
  • 1
  • PS: Please dont spam tags, they are there to attract the right audience for your question, not to gather an angry mod – RiggsFolly Apr 05 '23 at 10:20
  • Running the one query to get all the results, and then looping to query the exact same things again - but one row at a time - makes absolutely no sense at all. You could just loop through the original result set...that would be quicker, more efficient and require less code. – ADyson Apr 05 '23 at 12:15

1 Answers1

0

Solution

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