CC Search Function

status260 23 Jan, 2012
Dear Greyhead and CC community,

I have reviewed: How to add a Search to a Connectivity form!


My problem is I have 10 to 12 servers storing data in one table. I need to limit the results to ServerID and before committing to a search only show the table with a specific ServerID.


Prior to adding the search function I was able to display data using:
WHERE SQL
WHERE `ServerId` = 8


but now that I've added the search function when I commit to a search the search breaks.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE logSoldierName LIKE 'spookify4%'' at line 3 SQL=SELECT count(*) FROM tbl_chatlog WHERE `ServerId` = 7 WHERE logSoldierName LIKE 'spookify4%'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE logSoldierName LIKE 'spookify4%'' at line 3 SQL=SELECT count(*) FROM tbl_chatlog WHERE `ServerId` = 7 WHERE logSoldierName LIKE 'spookify4%'



Here is what Im using.

WHERE SQL:

WHERE `ServerId` = 8

<?php
$search_array = array('logSoldierName');
$where = array();
foreach ( $search_array as $search ) {
  $value = JRequest::getVar($search, '' , 'post');
  if ( $value ) {
    $where[] = " $search LIKE '$value%' ";
  }
}
if ( !empty($where) ) {
  echo " WHERE ".implode(' AND ', $where);
}?>


HEADER
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="0">
      <tbody>
        <tr>
<td>
<div style="float:left">
Search for a <b>soldier name</b>: <input type="text" name="logSoldierName">
<input type="submit" value="Search" name="undefined" style='width:80px; color:#cccccc; background-color:#222222; cursor:pointer;' /></div>   
</td></tr>
</tbody>
</table>


Thank you kindly for any assistance you may provide.

Regards,

Status260
GreyHead 23 Jan, 2012
Hi status260,

The output of the WHERE box needs to be a well-formed MySQL WHERE statement. You can't just stick the extra code on the front and expect it to work correctly. The error messages hows you that the result is
WHERE `ServerId` = 7 WHERE logSoldierName LIKE 'spookify4%' 
and you need it to be
WHERE `ServerId` = 7 AND `logSoldierName` LIKE 'spookify4%' 


Bob
status260 23 Jan, 2012
Right.

My problem is:

A) I am a novice at script writing.
B) I need the to select and display a specific ServerID before committing to a search.
This topic is locked and no more replies can be posted.