Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: searching help

  1. #1
    Regular Coder
    Join Date
    Dec 2002
    Location
    UK
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

    searching help

    Im trying to search two columns in a table. They are called author and code_name

    I use an if statement to see what the user wants to search for, but the variables always get set to the second if

    So when I try and search by author I get my error message "Please do not use weird characters"

    Heres the code I am using
    PHP Code:
    if (isset($_POST['user'])) {
    $searchby $_POST['user'];
    $setting "user";
    }
    if(isset(
    $_POST['codename'])) {
    $searchby $_POST['codename'];
    $setting "codename";
    }
        
    // Check for illegal characters
        
    if(!eregi("[a-zA-Z_0-9]",$searchby)){
        echo 
    "Please do not use weird characters.";
        exit;
        }

        include (
    "db.php");
        if (
    $setting == "user") {
        
    $query "SELECT * FROM code WHERE author LIKE '%$searchby%' ORDER BY code_id DESC";
        } else if (
    $setting == "codename") {
        
    $query "SELECT * FROM code WHERE code_name LIKE '%$searchby%' ORDER BY code_id DESC";
        }
        
    $result mysql_query($query);
        
    $numrows mysql_num_rows($result); 
    Any ideas?

  • #2
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    It is doing that because when you submit the form it sets both $_POST['user'] and $_POST['codename']. You could use this to get around that

    PHP Code:
     if (isset($_POST['user']) && $_POST['user'] != "") { 
    $searchby $_POST['user']; 
    $setting "user"
    } elseif (isset(
    $_POST['codename']) && $_POST['codename'] != "") { 
    $searchby $_POST['codename']; 
    $setting "codename"
    } else {
    //redirect back to search page because nothing is set

    Last edited by SeeIT Solutions; 06-19-2005 at 05:41 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •