Insert MySQL with php into a html a list

WaterHearts :

I am currently working on a website have ran into an issue. I am trying to insert data from my database into a list. Currently I run a problem where either I get the error:

Undefined variable: results in C:\xampp\htdocs\portfolio\index.php on line 65 or nothing outputs.

I know the issue not with query as I have tested it in phpmyAdmin and it works fine. If someone could point what is wrong and how to fix it, that would be awesome. Thank you.

Below is my code: main.php

<?php
if(isset($_POST['ass1'])){
    $courseNumber = "CS3800";
    $projectid = "PROJ0001";
    $results = get_courseNo_courseDes($db, $courseNumber, $projectid);
    print_r($results);
    unset($_POST['ass1']);
    }
 ?>


  <form action="ass1.php" method="post" name="a1">
  <input type="hidden" name="ass1">
 <ul name="list1">
 <?php
    foreach($results as $result){
    $sresult = $result['course_number'];
    echo"<li value='$sresult'>$sresult</option>"; 
    }
    ?>
    </ul>

    <button type="submit" value="Learn More About This Project" name="ass1">LearnMore About This Project</button>
    </form>

Here is my function in functions.php

function get_courseNo_courseDes($db, $courseNumber, $projectid){
  $query = "SELECT courses.course_name, project_description from projects inner join courses on projects.course_number = courses.course_number where courses.course_Number = :courseNumber and project_id = :projectid";
$statement = $db->prepare($query);
 $statement->bindValue(':courseNumber', $courseNumber);
 $statement->bindValue(':projectid', $projectid);
 $statement->execute();
 $result = $statement->fetchAll(PDO::FETCH_ASSOC);
 $statement->closeCursor();
 return $result;
}
danblack :

$results isn't set unless ass1 is POSTed.

foreach($results as $result) occurs for the non-ass1 POSTED query and therefore this error occurs.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=14054&siteId=1