belbin09 Posted December 16, 2017 Share Posted December 16, 2017 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> Quote Link to comment https://forums.phpfreaks.com/topic/305921-need-help-getting-the-script-to-search-the-database-to-find-the-record-that-matches-the-users-selection/ Share on other sites More sharing options...
requinix Posted December 16, 2017 Share Posted December 16, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/305921-need-help-getting-the-script-to-search-the-database-to-find-the-record-that-matches-the-users-selection/#findComment-1554697 Share on other sites More sharing options...
belbin09 Posted December 16, 2017 Author Share Posted December 16, 2017 (edited) 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 Edited December 16, 2017 by belbin09 Quote Link to comment https://forums.phpfreaks.com/topic/305921-need-help-getting-the-script-to-search-the-database-to-find-the-record-that-matches-the-users-selection/#findComment-1554705 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.