i have a form that searches only text based on 2 criteria. 1st criteria is the user selects from a drop down. The drop down is a table in the database. 2nd criteria is what the user enters in the text box and then clicks to submit. SO whatever the user types in the text will search in the drop down choice table. There are no validations yet needed unless you think i need it.

Here is my code so far for the FORM:

?php $page_title = "Search"; ?>
<?php include "header.php"; ?>


<table border=0 cellpadding=2 cellspacing=0 width=900 align=center>
<tr align=right bgcolor=#ECE5B6>
<td>
<form target="_self" action="search.php" method="post"><b>
Search by:
<select name="drpdb">
<option value="RequesterFullName">Requester Name</option>
<option value="PhysicalServerName">Server Name</opton>
<option value="Create_Date">Create Date</optiuon>
&nbsp;
</select>
Search: <input name="searchtxt" type="text">
<input type="submit" name="dosearch" value="Search">
</form></td></tr><b>
</table>

NO ERRORS , but please point out any problems i may here if you see it, thanks. Im stuck on the search.php file. Should i do a conditional statement, case or function. 2nd im not sure how the SQL query will be. Im using odbc to connect. HELP please

Recommended Answers

All 18 Replies

to get this done right you need to learn a bit of mysql, and learn how to call mysql query. I suggest w3school. We can give you the code but you will not be learning anything, So i suggest you learn a bit more of php and mysql and start doing it yourself and then if you have any error or if you are stuck post your PHP code here and we will help you.

Sure no problem, i have decided to with a conditional statements for this form;

Here is a copy of the code , just a snipet its reporting an error of

Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in "'""search.php on line 19

#Form Execution and Results
 if($_GET['drpdb']=="RequesterFullName"){
    [B]$qa="SELECT SRMTicketNumber, Create_Date FROM Storage_Request WHERE RequesterFullName LIKE  $_GET["searchtxt"]' ";[/B]
    $result=odbc_exec($conn,$qa);
    }
while(odbc_fetch_row($result)){
    $RequesterFullName = odbc_result_all($result, 1);
    $Create_Date = odbc_result_all($result, 2);
    print("$SRMTicketNumber $Create_Date\n");

else if($_GET['drpdb']=="PhysicalServerName"){
    $qa="SELECT SRMTicketNumber, Create_Date FROM Storage_Request WHERE PhysicalServerName LIKE  '($_GET['searchtxt'])'  ";
    $result=odbc_exec($conn,$qa);
    }
while(odbc_fetch_row($result)){
    $PhysicalServerName = odbc_result_all($result, 1);
    $Create_Date = odbc_result_all($result, 2);
    print("$SRMTicketNumber $Create_Date\n");

}
?>

As you can see there a 2 different sql statements im trying, neither works. Line 19 is bolded. What is wrong with my sql query? Should i use conditionals to accomplish this? How do i display it, any help will be appreciated and thanks.

if thats the error replace line 19 with this

$qa="SELECT SRMTicketNumber, Create_Date FROM Storage_Request WHERE RequesterFullName LIKE ".$_GET['searchtxt']."";

Yes looks better no error BUT no results show up. I changed some variable but all in the same db/table. Could it be the way im displaying it incorrectly?

#Form Execution and Results
 if($_POST["drpdb"]=="[B]AppRequestID[/B]"){
    $query="SELECT [B]AppRequestID[/B], Create_Date FROM  ILM_Storage_Request_MAIN3 WHERE AppRequestID  LIKE  ". $_POST["searchtxt"] ." " ;}
    $result = odbc_exec($conn,$query); 

while(odbc_fetch_row($result)){
    [B]$AppRequestID[/B] = odbc_result_all($result, 1);
    $Create_Date = odbc_result_all($result, 2);
    print("[B]$AppRequestID[/B]$Create_Date\n");
    }

if thats the error replace line 19 with this

$qa="SELECT SRMTicketNumber, Create_Date FROM Storage_Request WHERE RequesterFullName LIKE ".$_GET['searchtxt']."";

apocden was right. just add % in youre LIKE ".%$_GET%." or
LIKE %".$_GET."%

I have tried something else and still nothing appears no error but nothing appears... any suggestions

$stxt =  $_POST["searchtxt"];
$sdrp = $_POST["drpdb"];

echo "You are currently searching for: $stxt ";

#Form Execution and Results
 if($sdrp == "AppRequestID"){
    $query="SELECT AppRequestID, Create_Date FROM  ILM_Storage_Request_MAIN3 WHERE AppRequestID = '$stxt' ";
    $result = odbc_exec($conn,$query); 
    }
while(odbc_fetch_row($result)){
    $AppRequestID = odbc_result_all($result, 1);
    $Create_Date = odbc_result_all($result, 2);
    print("$AppRequestID $Create_Date\n");
    }
?>

Did you try to echo youre query? if not try to echo youre select query
$query="SELECT AppRequestID, Create_Date FROM ILM_Storage_Request_MAIN3 WHERE AppRequestID = '$stxt' ";
echo $query;
if youre $txt is not empty and it contain to what you put in youre input,but still no output,then the problem somewhere in AppRequestID = '$stxt' "; or maybe in youre print

Thanks kasakit, i finally got it to show up.. but for some reason i cant display single entries if i was looking for example a apprequestid? If the apprequest id has more than one or 3 entry it will run the search and only display 2 out of the 3 entries. If appreequest id has only 1 entry it will not find and display it? Do you know why is that? im using odbc connecting to a mssql db. I end using the following code

$stxt=$_POST["searchtxt"]

$query="SELECT AppRequestID, Create_Date FROM ILM_Storage_Request_MAIN3 WHERE AppRequestID LIKE '%$stxt%' ";

When i do a MS Query in ms excel, i do see all 3 records regardless of the other fields in the table.

example of how like % work.
1) like '%$stxt' for example you put "a123" in youre search box
it will search all youre records that END with a123.

2)like '$stxt%' for example you put "a123" in youre search box
it will search all youre records that START with a123.

3)like '%$stxt%' for example you put "a123" in youre search box
it will search all youre records that CONTAIN with a123. EVERYTHING its either in the end,last or in the middle.


if you are looking for a single AppRequestID its better to use where claus

hope that help

There are no validations yet needed unless you think i need it.

Beware of SQL injections. At least escape your GET/POST stuffs before using them

anyway, did you include youre while condition in youre if?if not just give it a try to include inside youre if statement.

Thanks again ksakit, but i am using a where clause i have tried a combonation of where but nothing only shows 2 out of 3 in the same colum.. There are no difference i dont understand this one

how do i escape the _POST or _GET? i have resarchd but dont get it, thanks

another example if there are 1 entry that match the criteria it spits out 'No Rows Found 0' however i put in my code if no match in the query print 'N Records Found' so the difference is that there is a record otherwise it would spit out 'No Records Found'

2 issue now -
1. when displaying results; always minus 1 record, so if had 6 entries in the table will only show 5,
2. cannot search for only 1 entry/record in the database.
that happens to all my searches no matter which criteria the user selects.

how do i escape the _POST or _GET? i have resarchd but dont get it, thanks

I'm yet to find mysql_real_escape_string equivalent for odbc.
May I know why are you troubling with ODBC while MySQL is free and very capable and is smoothly supportted in PHP?

not my choice evstevemd. im all for smooth and fully supported trust me. thanks

how about paging or numbering? is there a simple solution to that if i have many recors i want to be able to number it at least maybe separated by page with no nuumbering is fine

it keeps printing the heading or field name as well could that be the reason its not showing 1 of the results from the query. Could i get rid of the field name displayed?

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.