Sukhrat Arziyev Sukhrat Arziyev - 11 days ago 5
MySQL Question

Cannot access all elements given in the database

Here is the code. It perfectly displays all of the elements that I want. But when I click "enroll" it enrolls only one element for any chosen element. What is the possible problem?

Here's the code:

if(array_key_exists("id", $_SESSION)) {

include("connection.php");

$query = "SELECT * from `classes`";



echo '<div id="accordion" role="tablist" aria-multiselectable="true"> ';

$result = mysqli_query($link, $query);


$results = array();

$rows = array();

$queries = array();

while($row = mysqli_fetch_array($result)) {
array_push($rows, $row);
}

for ($i = 0; $i < count($rows); $i ++) {

$name = $rows[$i]["name"];

$start_time = $rows[$i]["begTime"];

$end_time = $rows[$i]["endTime"];

$price = $rows[$i]["price"];

$places = $rows[$i]["placesAvailable"];

if (array_key_exists("enroll", $_POST)) {
$queries[$i] = "UPDATE `users` SET sectionID = ".$rows[$i]["ID"]." WHERE id = ".mysqli_real_escape_string($link, $_SESSION['id'])." LIMIT 1";

$results[$i] = mysqli_query($link, $queries[$i]);


}

echo '

<div class="card">
<div id = "section" class="card-header" role="tab" id="headingOne">
<h5 class="mb-0">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
'.$name.'
</a>
</h5>
</div>

<div id="collapse" class="collapse in" role="tabpanel" aria-labelledby="headingOne">
<div class="card-block">
'.$name.'
<p> <strong> Time: </strong>'.$start_time.' - '.$end_time.' </p>
<p> <strong> Price: </strong>'.$price.' </p>
<p> <strong> Places available: </strong>'.$places.' </p>
<form action="" method = "post">
<input class="btn btn-success" type="submit" name = "enroll" value = "Enroll">
</form>
</div>
</div>
</div>

';
}

echo '</div>';

}

Answer

Your logic was not quite correct because you were not sending the desired course id to the script together with the submitted enroll button. I have adapted your code to do so. This logic will only work if your user/student should enroll in only one class at the time.

if(array_key_exists("id", $_SESSION)) {

include("connection.php");

        if (array_key_exists("enroll", $_POST)) {
            $querie = "UPDATE `users` SET sectionID = ".mysqli_real_escape_string($link, $_POST["ID"])." WHERE id = ".mysqli_real_escape_string($link, $_SESSION['id'])." LIMIT 1";

            mysqli_query($link, $queries[$i]); 
        }


    $query = "SELECT * from `classes`";


    echo '<div id="accordion" role="tablist" aria-multiselectable="true"> ';

    $result = mysqli_query($link, $query);

    while($row = mysqli_fetch_array($result)) {

        $name = $row["name"];

        $start_time = $rows["begTime"];

        $end_time = $rows["endTime"];

        $price = $rows["price"];

        $places = $rows["placesAvailable"];



        echo '

            <div  class="card">
                <div id = "section" class="card-header" role="tab" id="headingOne">
                    <h5 class="mb-0">
                        <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                            '.$name.'
                        </a>
                    </h5>
                </div>

                <div id="collapse" class="collapse in" role="tabpanel" aria-labelledby="headingOne">
                    <div class="card-block">
                    '.$name.'
                        <p> <strong> Time: </strong>'.$start_time.' - '.$end_time.' </p>
                        <p> <strong> Price: </strong>'.$price.' </p>
                        <p> <strong> Places available: </strong>'.$places.' </p>
                        <form action="" method = "post">
                            <input type="hidden" name="ID" value="'.$row["ID"].'" />
                            <input class="btn btn-success" type="submit" name = "enroll" value = "Enroll">
                        </form>
                    </div>
                </div>
            </div>

        ';
    }

    echo '</div>';

}
Comments