php - like or not like from $_GET

I have a form which uses http get to pass on search parameters to then do an sql query.
Select fields:
<br>
Field 1  
<select name="search_field1" id="search_field1">
<option value="subject">Subject</option>
<option value="location">Location</option>
<option value="ref_no">Reference Number</option>
<option value="requested_by">Requested by</option>
</select>
 
<select id="logical_operator1" name="logical_operator1">
<option selected value="like">contains</option>
<option value="nlike">does not contain</option>
</select>
Search term:
<input type="text" name="search_term1" id="search_term1">
 
<select name="search_field2" id="search_field2">
<option value="subject">Subject</option>
<option selected value="location">Location</option>
<option value="ref_no">Reference Number</option>
<option value="requested_by">Requested by</option>
</select>
 
<select id="logical_operator2" name="logical_operator2" >
<option value="like">contains</option>
<option value="nLike">does not contain</option>
<input type="text" name="search_term2" id="search_term2" value="">
on theresults.php
it grabs the parameters and then searches if a field is set to then generate thesql
query
$subject = $_GET["subject"];
$location = $_GET["location"];
$ref_no = $_GET["ref_no"];
$requested_by = $_GET["requested_by"];
$sql_prefix= "select Appointment.jobID,
Appointment.subject, Appointment.location, Appointment.ref_no
where";
if ( isset( $subject ) ) {
$sql_suffix.="`subject` like '%$subject%' ";
} else {
$sql_suffix.="`subject` like '%' "; //set to all subjects
}
if ( isset( $location ) ) {
$sql_suffix.= "and" . " `location` like '%$location%'";
} if ( isset( $ref_no ) ) {
$sql_suffix.="and" . " `ref_no` like '%$ref_no%'";
} if ( isset( $requested_by ) ) {
$sql_suffix.="and" . " `requested_by` $operator '%$requested_by%'";
}
$sql = $sql_prefix." ".$sql_suffix . " order by start asc";
Adding the"not like"
parameter for the sql query has made this challenging. I need to make the query change to"not like"
if the user selected that, but it needs to match the search term they selected. Since there can only be one of each parameter, I originally made this so it would just add the "and" plus the next term, plus"like"
.
How can I make this sql query say either"like"
or"not like"
according to the user's input? Do I need some kind of loop?
Answer
Solution:
<?php
if ($_GET) {
$typeFields = array("subject", "location", "ref_no", "requested_by");
$sql_prefix= "select Appointment.jobID,
Appointment.subject, Appointment.location, Appointment.ref_no
where";
$sql_suffix = "";
$flag = false;
for ($i=1; isset($_GET["search_field".$i]); $i++) {
for ($j=0; $j < count($typeFields); $j++) {
if ($typeFields[$j] == $_GET["search_field".$i]) {
$lop = "like";
if ($_GET["logical_operator".$i] == "nLike") {
$lop = "not like";
}
if ($flag) { $sql_suffix.="and"; } else { $flag = true; }
$sql_suffix.="`".$_GET["search_field".$i]."` ".$lop." '%". $_GET["search_term".$i]."%' ";
break;
}
}
}
$sql = $sql_prefix." ".$sql_suffix . " order by start asc";
echo $sql."<br><br><br>";
}
?>
Select fields:
<br>
Field 1
<form action="/index.php" method="GET">
<select name="search_field1" id="search_field1">
<option value="subject">Subject</option>
<option value="location">Location</option>
<option value="ref_no">Reference Number</option>
<option value="requested_by">Requested by</option>
</select>
<select id="logical_operator1" name="logical_operator1">
<option selected value="like">contains</option>
<option value="nlike">does not contain</option>
</select>
Search term:
<input type="text" name="search_term1" id="search_term1">
<select name="search_field2" id="search_field2">
<option value="subject">Subject</option>
<option selected value="location">Location</option>
<option value="ref_no">Reference Number</option>
<option value="requested_by">Requested by</option>
</select>
<select id="logical_operator2" name="logical_operator2" >
<option value="like">contains</option>
<option value="nLike">does not contain</option>
</select>
<input type="text" name="search_term2" id="search_term2" value="">
<input type="submit" value="send">
</form>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: fastcgi sent in stderr: "primary script unknown" while reading response header from upstream
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.