I'm working on a radio button that would trigger an "exact phrase" or an "any phrase" The thing is the results returned were similar when either the radio was selected.. Why is it so?
What i'm trying to do here is
When exactPhrase is selected:
records like"there is a 2mm crack" will be returned, whenever i do a search on 2mm crack
When anyPhrase is selected:
records like"there is a 1mm crack" won't be displayed , whenever i do a search on 2mm crack
$Keyword = (isset($_GET['Keyword'])) ? $_GET['Keyword'] : '';
$trimmedKeyword = trim($Keyword);
$trimmedKeyword = stripslashes($trimmedKeyword);
$trimmedKeyword = str_replace('"', '', $trimmedKeyword);
//trim whitespace from the stored variable
$trimmedKeyword = preg_replace('#\s+#',' ',$trimmedKeyword);
$trimmed_arrayKeyword = explode(" ",$trimmedKeyword);
// Build SQL Query for each keyword entered
foreach ($trimmed_arrayKeyword as $trimmKeyword => $word){
if(strtolower($word)){
$trimmed_arrayKeyword[$trimmKeyword] = "/\b(?<!<)($word)(?!>)\b/i";
}//end if
else $trimmed_arrayKeyword[$trimmKeyword] = '/\b('.preg_quote($word,'/').')\b/i';
if (isset($_GET['Condition']) == "exactPhrase")
$queryKeyword = "Select * from report Where AircraftTailNo LIKE '%$trimmedKeyword%' OR DefectInfo LIKE '%$trimmedKeyword%' OR Rectifications LIKE '%$trimmedKeyword%' OR Comments LIKE '%$trimmedKeyword%' OR RelatedDoc LIKE '%$trimmedKeyword%'";
else
$queryKeyword = "Select * from report Where AircraftTailNo REGEXP '[[:<:]]{%$word}[[:>:]]' OR DefectInfo REGEXP '[[:<:]]{$word}[[:>:]]' OR Rectifications REGEXP '[[:<:]]{$word}[[:>:]]' OR Comments REGEXP '[[:<:]]{$word}[[:>:]]' OR RelatedDoc REGEXP '[[:<:]]{$word}[[:>:]]'";
$numresultsKeyword =mysql_query ($queryKeyword);
$rowKeyword= mysql_fetch_array ($numresultsKeyword);
//get the results in term of REPORTID and store it in $adid_array
do{
$adid_array[] = $rowKeyword[ 'ReportID' ];
}
while($rowKeyword= mysql_fetch_array($numresultsKeyword));
} //end foreach
<td colspan="2">
<?php $Keyword = (isset($_GET['Keyword'])) ? stripslashes($_GET['Keyword']) : ''; ?>
<input name="Keyword" type="text" id="Keyword2" value="<?php echo $Keyword; ?>" size="20"/>
<br>
<input type='radio' value='anyPhrase' checked name='condition'>
Any Phrase <br>
<input type='radio' value='exactPhrase' name='condition'>
Exact Phrase</td>
i echo the query
This is displayed when i checked either of the box
SELECT FROM report WHERE AircraftTailNo REGEXP '[[:<:]]2mm[[:>:]]' OR DefectInfo REGEXP '[[:<:]]2mm[[:>:]]' OR Rectifications REGEXP '[[:<:]]2mm[[:>:]]' OR Comments REGEXP '[[:<:]]2mm[[:>:]]' OR RelatedDoc REGEXP '[[:<:]]2mm[[:>:]]'SELECT FROM report WHERE AircraftTailNo REGEXP '[[:<:]]crack[[:>:]]' OR DefectInfo REGEXP '[[:<:]]crack[[:>:]]' OR Rectifications REGEXP '[[:<:]]crack[[:>:]]' OR Comments REGEXP '[[:<:]]crack[[:>:]]' OR RelatedDoc REGEXP '[[:<:]]crack[[:>:]]'
i edited my codes from this
if (isset($_GET['Condition']) == "exactPhrase")
$queryKeyword = "Select * from report Where AircraftTailNo LIKE '%$trimmedKeyword%' OR DefectInfo LIKE '%$trimmedKeyword%' OR Rectifications LIKE '%$trimmedKeyword%' OR Comments LIKE '%$trimmedKeyword%' OR RelatedDoc LIKE '%$trimmedKeyword%'";
else
$queryKeyword = "Select * from report Where AircraftTailNo REGEXP '[[:<:]]{%$word}[[:>:]]' OR DefectInfo REGEXP '[[:<:]]{$word}[[:>:]]' OR Rectifications REGEXP '[[:<:]]{$word}[[:>:]]' OR Comments REGEXP '[[:<:]]{$word}[[:>:]]' OR RelatedDoc REGEXP '[[:<:]]{$word}[[:>:]]'";
to:
if($radio == "exactPhrase"){
$sql = "AircraftTailNo LIKE '%$trimmedKeyword%' OR DefectInfo LIKE '%$trimmedKeyword%' OR Rectifications LIKE '%$trimmedKeyword%' OR Comments LIKE '%$trimmedKeyword%' OR RelatedDoc LIKE '%$trimmedKeyword%'";
}
if($radio == "anyPhrase"){
$sql = "AircraftTailNo REGEXP '[[:<:]]{$word}[[:>:]]' OR DefectInfo REGEXP '[[:<:]]{$word}[[:>:]]' OR Rectifications REGEXP '[[:<:]]{$word}[[:>:]]' OR Comments REGEXP '[[:<:]]{$word}[[:>:]]' OR RelatedDoc REGEXP '[[:<:]]{$word}[[:>:]]'";
}
It was able to search for exact words..
The query that was displayed when i select radio button "Any Phrase"
SELECT * FROM report WHERE AircraftTailNo LIKE '%3mm crack%' OR DefectInfo LIKE '%3mm crack%' OR Rectifications LIKE '%3mm crack%' OR Comments LIKE '%3mm crack%' OR RelatedDoc LIKE '%3mm crack%'SELECT * FROM report WHERE AircraftTailNo LIKE '%3mm crack%' OR DefectInfo LIKE '%3mm crack%' OR Rectifications LIKE '%3mm crack%' OR Comments LIKE '%3mm crack%' OR RelatedDoc LIKE '%3mm crack%'
there are such errors even before i do a search, how should i resolve it?
Notice: Undefined variable: sql in c:\inetpub\wwwroot\sip\review.php on line 242
SELECT * FROM report WHERE 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 '' at line 1
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\inetpub\wwwroot\sip\review.php on line 246
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\inetpub\wwwroot\sip\review.php on line 251
Line 246 - $rowKeyword= mysql_fetch_array ($numresultsKeyword);
Line 251 - while($rowKeyword= mysql_fetch_array($numresultsKeyword));
$Keyword = (isset($_GET['Keyword'])) ? $_GET['Keyword'] : '';
$trimmedKeyword = trim($Keyword);
$trimmedKeyword = stripslashes($trimmedKeyword);
$trimmedKeyword = str_replace('"', '', $trimmedKeyword);
//trim whitespace from the stored variable
$trimmedKeyword = preg_replace('#\s+#',' ',$trimmedKeyword);
$trimmed_arrayKeyword = explode(" ",$trimmedKeyword);
// Build SQL Query for each keyword entered
foreach ($trimmed_arrayKeyword as $trimmKeyword => $word){
if(strtolower($word)){
$trimmed_arrayKeyword[$trimmKeyword] = "/\b(?<!<)($word)(?!>)\b/i";
}//end if
else $trimmed_arrayKeyword[$trimmKeyword] = '/\b('.preg_quote($word,'/').')\b/i';
if($radio == "exactPhrase"){
$sql = "AircraftTailNo LIKE '%$trimmedKeyword%' OR DefectInfo LIKE '%$trimmedKeyword%' OR Rectifications LIKE '%$trimmedKeyword%' OR Comments LIKE '%$trimmedKeyword%' OR RelatedDoc LIKE '%$trimmedKeyword%'";
}
if($radio == "anyPhrase"){
$sql = "AircraftTailNo REGEXP '[[:<:]]{$word}[[:>:]]' OR DefectInfo REGEXP '[[:<:]]{$word}[[:>:]]' OR Rectifications REGEXP '[[:<:]]{$word}[[:>:]]' OR Comments REGEXP '[[:<:]]{$word}[[:>:]]' OR RelatedDoc REGEXP '[[:<:]]{$word}[[:>:]]'";
}
$queryKeyword = "SELECT * FROM report WHERE $sql";
echo $queryKeyword;
$numresultsKeyword =mysql_query ($queryKeyword);
echo mysql_error();
$rowKeyword= mysql_fetch_array ($numresultsKeyword);
//get the results in term of REPORTID and store it in $adid_array
do{
$adid_array[] = $rowKeyword[ 'ReportID' ];
}
while($rowKeyword= mysql_fetch_array($numresultsKeyword));
} //end foreach
<input name="Keyword" type="text" id="Keyword2" value="<?php echo $Keyword; ?>" size="20"/>
<br>
<input type='radio' value='anyPhrase' checked name='radiobutton'>
Any Phrase <br>
<input type='radio' value='exactPhrase' name='radiobutton'>
Exact Phrase</td>