Connecting Tech Pros Worldwide Forums | Help | Site Map

Search function in PHP

Familiar Sight
 
Join Date: Jan 2008
Posts: 199
#1: Mar 12 '08
In my web application there is a table user can see the data(id,date phone number etc.). But i want to add add search facility there(Search by ID/phone no). My code is not working and saying "Could not execute query: SELECT * FROM 'xxx' WHERE 'xxx' LIKE Connect.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 ''xxx' WHERE 'xxx' LIKE Connect' at line 1"

This is my code
----------------------------[php]
<form target="mainFrame" action="search.php" method="post">
Search By
<select name="col">
<option value="Author">District</option>
<option value="xxx">xxx</option>
</select>
<input name="search" type="text">
<input type="submit" name="submit" value="search">
</form>


<?php

include 'dbconnect.php';

$db=mysql_select_db(mmm,$dbh) or die("Could not select database");

if($_POST['col']=="yyy"){
$q="SELECT * FROM 'xxx' WHERE 'xxx' LIKE $_POST[search]" ;
}
if($_POST['col']=="xxx"){
$q="SELECT * FROM 'xxx' WHERE 'xxx' LIKE $_POST[search]" ;}

$result=mysql_query($q) or die ("Could not execute query: $q.".mysql_error());
while($row=mysql_fetch_array($result)){
$ID=$row["ID"];
$xxx=$row["xxx"];
$PhoneNo=$row["PhoneNo"];
$District=$row["District"];
$date=$row["date"];
$vDescription=$row["Description"];?>
<tr class="row">

</td>
</tr>
<?php }?>[/php]

Please enclose any code within the proper code tags. See the Posting Guidelines on how to do that.

MODERATOR

Newbie
 
Join Date: Mar 2008
Location: Hyderabad
Posts: 17
#2: Mar 12 '08

re: Search function in PHP


Quote:

Originally Posted by ghjk

In my web application there is a table user can see the data(id,date phone number etc.). But i want to add add search facility there(Search by ID/phone no). My code is not working and saying "Could not execute query: SELECT * FROM 'xxx' WHERE 'xxx' LIKE Connect.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 ''xxx' WHERE 'xxx' LIKE Connect' at line 1"

This is my code
----------------------------
<form target="mainFrame" action="search.php" method="post">
Search By
<select name="col">
<option value="Author">District</option>
<option value="xxx">xxx</option>
</select>
<input name="search" type="text">
<input type="submit" name="submit" value="search">
</form>


<?php

include 'dbconnect.php';

$db=mysql_select_db(mmm,$dbh) or die("Could not select database");

if($_POST['col']=="yyy"){
$q="SELECT * FROM 'xxx' WHERE 'xxx' LIKE $_POST[search]" ;
}
if($_POST['col']=="xxx"){
$q="SELECT * FROM 'xxx' WHERE 'xxx' LIKE $_POST[search]" ;}

$result=mysql_query($q) or die ("Could not execute query: $q.".mysql_error());
while($row=mysql_fetch_array($result)){
$ID=$row["ID"];
$xxx=$row["xxx"];
$PhoneNo=$row["PhoneNo"];
$District=$row["District"];
$date=$row["date"];
$vDescription=$row["Description"];?>
<tr class="row">

</td>
</tr>
<?php }?>

Hello,
try this in if condition
[PHP]$searchtext=$_post['search'];
$q="SELECT * FROM 'xxx' WHERE 'xxx' LIKE '%$searchtext%' " ;
[/PHP]
hadi00's Avatar
Newbie
 
Join Date: Mar 2008
Location: Syria
Posts: 9
#3: Mar 12 '08

re: Search function in PHP


You may also be sure to escape special chars with your Post variables, to be aware of sql injection attempts...
Reply