0

In my while loop I am able to correctly do the first part of the if statement but the ELSE will not work, I can get everything to work correctly so I get the string and I match it against the string in the db and if the string is correct I get the correct output too but if the string does not match I can't get the else statement to work so to just echo out that the string does not match.

if(isset($_POST['submit'])) {

$search_query = escape_string($_POST['search_query']);

$query = "SELECT client_id, client_name, status FROM clients WHERE client_id = '".$search_query."' ";
$result = mysqli_query($connection, $query);
if($result && !empty($search_query)) {

    while($code = mysqli_fetch_assoc($result)) {

        if($_POST['search_query'] === $code['client_id']) {
            echo $code['client_name'] . " " . $code['client_id'] . " " . $code['status'];   
        } else {
            echo $_POST['search_query'] . " ID does not exist!";
        }

    }

} 

}

This is the form:

<form action="search.php" method="post">

    <p>
        <input type="text" name="search_query" id="search" />
        <input type="submit" name="submit" value="SEARCH" />
    </p>

</form>
11
  • 3
    Your question makes no sense. You need to clarify what is actually happening. Also, what are the values of $_POST['search_query'] and $code['client_id']? Commented Feb 18, 2015 at 16:31
  • I apologize, I have a search field where the user inputs a code and if that code exists in the db and matches his code it will echo the client name, id and status. the code to match the user input with the code in the db works fine but the problem is in the else statement so if the code didn't match. Commented Feb 18, 2015 at 16:35
  • I'm not seeing anything wrong with the IF or the ELSE. Commented Feb 18, 2015 at 16:39
  • I would put a break statement after the matching result so that the loop does not continue processing once a match is found Commented Feb 18, 2015 at 16:41
  • @BigScar very true, I don't understand why it is not going through the else statement if the match is false. Commented Feb 18, 2015 at 16:45

1 Answer 1

1

Your query returns results where $search_query matches client_id, which means that the "else" part of your if/else statement never applies. You need to move it outside of the while loop.

if ( isset($_POST['submit']) ) {
    // By default there is no match
    $match = false;
    $search_query = escape_string($_POST['search_query']);
    if ( !empty($search_query) ) { // Why query in the first place if the search is empty?
        $query = "SELECT client_id, client_name, status FROM clients WHERE client_id = '".$search_query."' ";
        $result = mysqli_query($connection, $query);
        if ( $result ) {    
            while ( $code = mysqli_fetch_assoc($result) ) {
                // Store the matched data in an array so that is is easy to work with
                $match = array(
                    'client_name'   => $code['client_name'],
                    'client_id'     => $code['client_id'],
                    'status'        => $code['status']
                );
            }
        }
    }
    if ( is_array($match) ) {
        // match found  
    } else {
        // match not found
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I didn't think that php's IF . . . ELSE had stopped working.
this code works very well but now when i test for example if abosh15 is in the database it returns true which is correct but it also returns Abosh15 as true I need it to be an exact match only case-sensitive
got it to work with the exact match too, thank you both Dave and @BigScar

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.