Jump to content
Larry Ullman's Book Forums

Script 9.2/9.3 - Searching For A User - (By User_Id, Or By Name)


Recommended Posts

I would like to modify the search section of the above scripts so that in a single text box I can search by user id (student_no), first name or last name. In my entry form, I am using a drop-down list for the user to select the search criteria first before typing in the search details.

 

I have succeeded in searching by the student_no. However, I get the following error if I try to search by name:

 

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\celeste\handle_search_student2.php on line 37

 

 

Here is the code in the entry from:

 

 

<form action="handle_search_student2.php" method="get">
    <p>
        <label for="search_criteria">Search by:</label>
            <select name = "search_criteria" id="search_criteria">
                <option value="student_no" >Student no.</option>
                <option value="first_name" >First name</option>
                <option value="last_name" >Last name</option>                
            </select>
        
        <input type="character" name="search_item" size="20" maxlength="40" value="<?php if(isset($_POST['search_item'])) echo $_POST['search_item']; ?>" />
    </p>    
    <p><input type="submit" name="submit" value="Search for Student" maxlength="20" /></p>    
    <input type="hidden" name="submitted" value="TRUE" />
</form>

 

 

And here is part of the routine handling the form:

 

require_once ('../../mysqli_connect.php'); // Connect to the db.    
$errors = array(); // Initialize an error array.

//function declaration - Count no of Digits in Student No.
function count_digit($number) {
return strlen((string) $number);
}
    
// Check for a student no.:
if (empty($_REQUEST['student_no'])&&empty($_REQUEST['first_name'])&&empty($_REQUEST['last_name']) ) {
    $errors[] = 'Please enter at least ONE search option.';
}    else    {

    if (!empty($_REQUEST['student_no']))    {
        $search_criteria = 'student_no';
        $sc = mysqli_real_escape_string($dbc, trim($_REQUEST['student_no']));        
    } elseif (!empty($_REQUEST['first_name']))    {
        $search_criteria = 'first_name';
        $sc = mysqli_real_escape_string($dbc, trim($_REQUEST['first_name']));        
    } else    {
        $search_criteria = 'last_name';
        $sc = mysqli_real_escape_string($dbc, trim($_REQUEST['last_name']));        
    }
    
    // Make the query:
    $q = "SELECT s.student_id, s.student_no, s.first_name, s.middle_name, s.last_name, s.gender, s.school, l.lab_name, c.country, s.email, s.campus FROM students AS s LEFT JOIN lab AS l USING (lab_id) LEFT JOIN countries AS c USING (ccode) WHERE $search_criteria = $sc";
    $r = @mysqli_query($dbc, $q);    // Run the query    
    
    // Count the no. of returned rows:
    $num = mysqli_num_rows($r);
        
    if ($num == 1) {    
        
 

I just cant seem to identify the cause of the error? Is there a better way to do this?

 

Regards.

 

Link to comment
Share on other sites

 Share

×
×
  • Create New...