Jump to content

Need help getting the script to search the database to find the record that matches the user's selection


belbin09

Recommended Posts

Hi, I am trying to read in the name and student number that the user entered and compare it to what is in the mysql database. My teacher suggested using found. However I am still not getting anything to echo out. Can someone lead me in the right direction?

 

next.php

 

<?php
 
require 'connect.php';
 
//linking up to the database
$link = mysqli_connect(HOST, USER, PASS, DB) or die (mysqli_connect_error());
 
//making a variable from the user data
$name = mysqli_real_escape_string ($link, $_POST["name"]);
$number = mysqli_real_escape_string ($link, $_POST["snumber"]);
$course = $_POST["pcourse"];
 
// select all from table student which show student name and number
$squery = "SELECT * FROM students";
$sresult = mysqli_query($link, $squery);
 
while ($srow = mysqli_fetch_array($sresult)) {
 
  if ($name == $srow['uid'] && $number == $srow['student']) {
 
    if ($found) {
 
    echo "$srow[uid] $srow[student]";
 
    } else {
 
    echo "Student does not exist";
 
    }
  }
}
 
mysqli_close ($link);
?>
 
<html>
<body>
<form action="index.php" method="post">
  <br>
    <input type = "submit" value="back" name="back">
</form>
</body>
</html>
 
This is my index.php that I use as my form
 

<!DOCTYPE html>
<html>
  <body>
    <h1>Course Selection</h1><br>
 
 
    <form action="next.php" method="post">
 
 
              Name: <input type="text" name="name" placeholder="Name" required="required" maxlength="50">
              <br><br>
 
              Student Number: <input type="text" name= "snumber" required="required" maxlength="9">
              <br><br>
 
        <?php
        //form
      require 'connect.php';
 
      $link = mysqli_connect(HOST, USER, PASS, DB) or die(mysqli_connect_error());
 
      echo "Select a course: <select name = \"pcourse\">\n";
 
      $query = "SELECT * FROM course";
      $result = mysqli_query($link, $query);
 
      while ($row = mysqli_fetch_array($result)) {
        echo "<option> $row[code=auto:0] $row[name] $row[max]</option><br>";
      }
 
      mysqli_free_result($results);
 
      mysqli_close ($link);
 
      echo " </select>\n";
 
 
      ?>
 
      <br><br>
      <input type = "submit" value="submit" name= "submit">
 
    </form>
 
    </body>
    </html>
Link to comment
Share on other sites

You're trying to use $found but don't set it anywhere. But even then your logic is quite wrong.

 

Do the search in the query.

"SELECT * FROM students WHERE uid = '$name' AND student = '$number'"
which I write according to that comparison you're making now that appears to have the variables backwards.

 

If the query produces any results at all then the student already exists. Sure, go ahead and output the student ID and name, but they're the same values entered by the user to do the search in the first place so it doesn't really mean much to repeat them back.

Link to comment
Share on other sites

Thank you. I'll try that. I am only trying to output the results to make sure it works. Later I will be writing the course and student number into a different table and then comparing those results to the users input to make sure they haven't already registered for that course

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.