1

I hate asking for help with something that seems like it should be obvious but I'm a little lost on this one:

I'm trying to set up code that will either search for a student's name, or just display a list of the most recent 100 student files.

The list code works fine, but whenever I try to run a search, it comes back with the correct student name, while the unique ID for each student will always be the wrong number. For example: bob, ID: 23, Jane, ID: 23, Jill: ID: 23 - when the results should really be bob, ID: 23, Jane, ID: 24, Jill: ID: 25

What am I missing?

<?php
if($_POST['Student_Name'])
    {
    // Select multiple records from the database and print them out..
    $Student_Name = $_POST['Student_Name']; 

    $users = $db->get_results("select * FROM Students WHERE Name LIKE '%$Student_Name%' order by ID DESC");
   echo "Search is on";


    } else {

    $users = $db->get_results("SELECT * FROM Students order by ID DESC LIMIT 100");
    echo "No Search engaged <HR>";
    }


    foreach ( $users as $user )   
    {
        echo "<div class=\"row\"><div class=\"col-sm-4\">";
        echo $user->Name;       
        echo "</div>";
        echo "<div class=\"col-sm-8\">";    
        echo "<form action=\"Students_Profile.php\"  method=\"post\" ><input type=\"hidden\" name =\"Student_ID\" value=".$user->ID." /><input type=\"submit\" value=\"View Student Profile\" /></form></div>";         
    } ?>
Full Stack Alien
  • 11,244
  • 1
  • 24
  • 37
  • Try changing the foreach to: foreach ( $users as $key => $user ) { var_dump($key, $user); What is the resutl of the var dump? – Guilherme Assemany Aug 07 '17 at 20:15
  • Could you print_r your $users before the foreach? – ednincer Aug 07 '17 at 20:21
  • Did you write `get_results`? I would guess that is your own function and only runs 1 fetch, additionally this is open to SQL injections. – chris85 Aug 07 '17 at 20:23
  • The indentations of this question also are off. – chris85 Aug 07 '17 at 20:28
  • I'll have to try these later today, thanks for the insights. I was also thinking of just separating the results depending on listing vs. searching. I'm not worried about injection yet, it's a locally based db, very beta... – Borkmeister Aug 08 '17 at 14:19

0 Answers0