skip to Main Content

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:xampphtdocsportfolioindex.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;
}

2

Answers


  1. $results isn’t set unless ass1 is POSTed.

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

    Login or Signup to reply.
  2. @WaterHearts Please refer to this tutorial.

    It is simply inserting data into the database.

    Please see this LINK

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search