could anyone provide code for me how to search data using column name.

example: if i have 2 columns called NAME,ID then i select NAME or ID and enter the keyword to search.
would greatly appreciate your help!

Recommended Answers

All 3 Replies

Simply you can do this with a simple if/elseif/else statement.
You just have to play with your variables and with your database fields.

<?php
    
    if (isset($_REQUEST['yourColumnValue1']) && $_REQUEST['yourColumnValue1'])
    {    
        $q = mysql_query("SELECT * FROM yourTable 
                          WHERE yourField LIKE '%".mysql_real_escape_string($_REQUEST['SearchValue'])."%' 
                          || yourField2 = '".mysql_real_escape_string($_REQUEST['SearchValue'])."' 
                          ORDER BY x, y");
    }
    elseif (isset($_REQUEST['yourColumnValue2']) && $_REQUEST['yourColumnValue2'])
    {    
        $q = mysql_query("SELECT * FROM yourTable 
                          WHERE yourField LIKE '%".mysql_real_escape_string($_REQUEST['SearchValue'])."%' 
                          || yourField2 = '".mysql_real_escape_string($_REQUEST['SearchValue'])."' 
                          ORDER BY x, y");
    }
    
    
?>
Member Avatar for diafol

There seems to be a lot of code duplication here. Also use POST or GET as opposed to REQUEST.

thanks guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.