-3

My search form is not working. It only works when I remove the 'search bar' called 'search' as I also have several drop down menus where users can fetch details from table. I simple added 'search' so users can straight away find specific information. But when I added the 'search' bar, then it doesn't work.

index.php

     <form name="formbrwse" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" enctype="multipart/form-data">
         <input type="text" name="search">
<td>
<input type="image" name="searchbtn" id"searchbtn" src="searchbtn.fw.png"
width="166" height="38"></td>

search.php

if(isset($_POST['searchbtn_x'])&&($_POST['searchbtn_x']!='')&&isset($_POST['search'])&&($_POST['search']!='')){
$search=isset($_POST['search'])?$_POST['search']:"";
$search=preg_replace("#[^0-9a-z]i#","",$search);
$category=isset($_POST['category'])?$_POST['category']:"";
$location=isset($_POST['location'])?$_POST['location']:"";
$name_ddasr=isset($_POST['name_ddasr'])?$_POST['name_ddasr']:"";
$opt=isset($_POST['opt'])?$_POST['opt']:"";
//dropdown menu below
if(isset($_POST['category'],$_POST['location'],
$_POST['name_ddasr'],$_POST['opt'])){
if($_POST['search']!=''&&$_POST['category']=='All Categories'&&$_POST['location']
=='Entire Place'&&$_POST['name_ddasr']=='For sale'&&$_POST['opt']=='vrecent'){
//DDmenu comparing ends here    
$advert = "SELECT * FROM usertable WHERE adtitle LIKE '%$search%' AND 
dcrptn LIKE '%$search%' AND optionsalerent='For sale'";
$result=$conn->query($advert);
}}}
2JZemco
  • 1
  • 2
  • maybe you have a dublicate name for a input. Can you post the full form with the submit button? – Rafael Shkembi Dec 10 '16 at 11:18
  • 3
    Spaghetti code... – Koen Hollander Dec 10 '16 at 11:19
  • Please format your code. Use spaces, emply lines, tabs. You can try Notepad++ or eclipse PDT. – code_angel Dec 10 '16 at 11:58
  • Indent your code propertly – Yaroslav Dec 10 '16 at 12:45
  • Hi code_angel...I'll check the answer soon.. tqs for the reply..will let u know later. – 2JZemco Dec 10 '16 at 13:57
  • Hi Koen....can you be more specific which part is spaghetti?...Maybe you can teach me..tqs – 2JZemco Dec 10 '16 at 13:59
  • Rafael..dont worry about the submit button.. its an image button... it works.. all along when i dont add the search bar.. its not working recently after i added the search bar.. thats all.. I hope u get what i mean..tqs – 2JZemco Dec 10 '16 at 14:00
  • @Koen tqs for the reply anyways. – 2JZemco Dec 10 '16 at 14:21
  • @Rafae tqs for the ply anyways – 2JZemco Dec 10 '16 at 14:21
  • your form seems incomplete; I don't see `` anywhere along with the other inputs. You also have syntax errors right here `$advert = "SELECT * FROM usertable WHERE adtitle LIKE '%$search%' AND dcrptn LIKE '%$search%' AND optionsalerent='For sale';` – Funk Forty Niner Dec 10 '16 at 14:48
  • also `get_result()` isn't available on all servers - as per http://php.net/manual/en/mysqli-stmt.get-result.php *"MySQL Native Driver Only Available only with mysqlnd"*. – Funk Forty Niner Dec 10 '16 at 14:50
  • hi@Fred-ii- The above codes are just partial extracted codes.. I purposely didn't post everything because the codes are quite too long with css added etc. The codes actually works if I don't include 'search'.The category, location. name_ddasr and opt are dropdown menus which works fine. But when I added 'search' to make it more specific, it wont work. I already corrected the syntax.. after sale...by adding double apostrophe..but still same ..tqs anyway for the reply. – 2JZemco Dec 10 '16 at 14:54
  • wow.. tqs @Fred-ii-...I didn't knew that about get_result(). I thought it was available on all servers..tqs.. – 2JZemco Dec 10 '16 at 14:56
  • I take it that my comment solved it for you then, about the use of `get_result()`? @2JZemco let me know so I can it as an answer so the question can be marked off as solved. – Funk Forty Niner Dec 10 '16 at 15:02
  • tqs @Fred-ii-....yup...your comment regarding the and syntax error.. everything is done actually.. but I still cannot get any result though.I'm used to using get_result...any suggestions to replace it?...probably this might be the reason causing the problem?.. – 2JZemco Dec 10 '16 at 15:10
  • if you go over the manual on that get_result() http://php.net/manual/en/mysqli-stmt.get-result.php you'll see examples to replace it with. There are also a few Q&A's on Stack you can have a look at http://stackoverflow.com/q/7133575/ - http://stackoverflow.com/q/7016169/ - http://stackoverflow.com/q/10752815/ and this one may be better http://stackoverflow.com/q/8321096/ or http://stackoverflow.com/q/26197031/ – Funk Forty Niner Dec 10 '16 at 15:23
  • btw, I didn't get any of your pings to me, since there should not be any characters following my name ;-) I only reloaded the comments to see if there were any new ones. make sure there is no characters to the left and/or right of my name. – Funk Forty Niner Dec 10 '16 at 15:24
  • @Fred-ii- Sorry for that..I added some letters before your name, my mistake. Ok, tqs, I will have a look on the manual. Btw, currently I'm only running on local host (xampp), I assume the get_result() suppose to work. I'll check it out first to see whats causing the problem. Tqs bro. – 2JZemco Dec 10 '16 at 15:44
  • @Fred-ii- Tqs for the advice Fred. You seem like a professional programmer. Cheers. – 2JZemco Dec 10 '16 at 15:55
  • @2JZemco Lord as my witness, I try ;-) so, have we got success? *Cheers* – Funk Forty Niner Dec 10 '16 at 15:56
  • @Fred-ii- I dont want to trouble u Fred.you've been a good help. I tried and tried but I still dont know how to install or enable mysqlnd(native driver), Lol. I think you are right. The problem was caused by get_result(). I've read the manuals, but still can't seem to get mysqlnd enabled. I'll try to work it out. – 2JZemco Dec 10 '16 at 17:49
  • @Fred-ii- Hi Fred.. I checked the PHP settings.. I have mysqlnd enabled by default.. so I think there's another problem ..hope there are other solutions..tqs btw for the previous help – 2JZemco Dec 11 '16 at 05:43
  • It shows client API library version : mysqlnd 5.0.10...then on another table.. it shows API externsion mysql,mysqlii,pdo_mysql..is this right?? – 2JZemco Dec 11 '16 at 05:50

1 Answers1

0

A double quote is missing after 'For sale'. It should be 'For sale'";

$advert = "SELECT * FROM usertable WHERE adtitle LIKE '%$search%' AND 
dcrptn LIKE '%$search%' AND optionsalerent='For sale';
code_angel
  • 1,517
  • 1
  • 11
  • 20