Your IP : 216.73.216.40


Current Path : /var/www/html/venkat/phphtdocs/
Upload File :
Current File : /var/www/html/venkat/phphtdocs/storeselection.php

<?php
include("fetchdropped.php");

$_backButton = '<div class="container"><form action="choice.php"><button type="submit">Go Back</button></form></div>';

if (!isset($_POST['choice'])) {
    echo '<h4>No choices selected.</h4>';
    echo $_backButton;
    exit;
}

$db = connectdb();

$username = $_SESSION['username'];
$courses = $_POST['choice'];
// print_r($courses);

$query = <<<EOF
select user_id from students where username='$username';
EOF;

$res = pg_query($db, $query);
$userid = pg_fetch_row($res)[0];

foreach ($courses as $coursename) {
    $query = <<<EOF
select course_id from courses where course_name='$coursename';
EOF;
    $res = pg_query($db, $query);
    $courseid = pg_fetch_row($res)[0];

    $query = <<<EOF
select course_name from courses where course_id in (select course_id from summer2019 
where user_id=$userid);
EOF;

    $res = pg_query($db, $query);

    $totalcredits = 0;

    while($x = pg_fetch_row($res)[0]) {
        // echo $x . '<br>';
        $creds = credits($x);
        $totalcredits += $creds;
        if ($x == $coursename) {
            echo 'Course <b>' . $coursename . '</b> already selected.';
            echo $_backButton;
            exit;
        }
    }
    // echo 'Total credits: ' . $totalcredits . '<br>';

    if (($totalcredits + credits($coursename)) > 12) {
        echo 'You have selected more than 12 credits with course choice <b>' . $coursename . '</b><br>';
        echo 'You may remove any other course to make room for this course.<br>';
        echo $_backButton;
        exit;
    }

    $query = <<<EOF
insert into summer2019 (user_id, course_id) values ($userid,$courseid);
EOF;

    $res = pg_query($db, $query);
    echo 'Added course choice <b>' . $coursename . '</b><br>';

    if(!$res) {
        echo pg_last_error($db);
    }
}

echo $_backButton;
?>