0

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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

oh okay I see, however when I remove the isset post, I get this error: Call to undefined function get_courseNo_courseDes() in C:\xampp\htdocs\portfolio\index.php:5 Stack trace: #0 {main} thrown in C:\xampp\htdocs\portfolio\index.php on line 5
functions.php isn't included in main.php
0

@WaterHearts Please refer to this tutorial.

It is simply inserting data into the database.

Please see this LINK

1 Comment

Thank you as well, this tutorial was very informative.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.