sql - Modify $select to allow for searching in php -


how modify $select function allow searching database when customer types in search text , clicks "search"? i'd able type in form field, , php automatically updates page form data dynamically , able define field search upon in database!

this letting me view customers table:

$select = $db->query("select * customers order id desc"); <?php         if (!$select->num_rows) {             echo '<p>', 'no records', '</p>';         }else{         ?>             <table border="1" width="100%">             <thead>                 <tr>                     <th>first name</th>                     <th>last name</th>                 </tr>             </thead>             <tbody>             <?php                 while ($row = $select->fetch_object()) {             ?>                 <tr>                     <td><?php echo $row->fname;?></td>                     <td><?php echo $row->lname;?></td> 

first of all, might try putting opening <?php before rather after first line of code... :-)

then modify code values html form - assume search term named q kind of this:

search: <input type="text" name="q" /> <input type="submit" name="search" /> 

then php script doing searching change query this:

$select = $db->query("select * customers fname '%$_request[q]%' or lname '%$_request[q]%' order id desc"); 

if entered "sam" end query looks this:

$select = $db->query("select * customers fname '%sam%' or lname '%sam%' order id desc"); 

do note showing simplest version constructing query. in fact should never user-supplied data. instead, should prepare , bind , execute statement. not filler @ end of answer - it's important. while testing may want see how works forming string above.

to answer "bonus points" - can update current page including search form on same page php script displays results. process php code if submit button has been pressed. whole script (in simple form) might this:

search: <input type="text" name="q" /> <input type="submit" name="search" /> <?php if (isset($_request['search'])) {     # query     # loop through results, printing them off in appropriate html } 

to answer "bonus bonus" points, set pull-down ("select" in html) values of options actual field names , display human-readable form of that. if select had name "field_name" modify query this:

$select = $db->query("select * customers $_request[field_name] '%$_request[q]%' order id desc"); 

do note cannot prepare/bind column name, have careful validate field (probably checking exists in list of acceptable values).


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -