Search function in PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ghjk
    Contributor
    • Jan 2008
    • 250

    Search function in PHP

    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="mainFra me" 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_selec t_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_q uery($q) or die ("Could not execute query: $q.".mysql_erro r());
    while($row=mysq l_fetch_array($ result)){
    $ID=$row["ID"];
    $xxx=$row["xxx"];
    $PhoneNo=$row["PhoneNo"];
    $District=$row["District"];
    $date=$row["date"];
    $vDescription=$ row["Descriptio n"];?>
    <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
  • Ravigandha
    New Member
    • Mar 2008
    • 17

    #2
    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="mainFra me" 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_selec t_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_q uery($q) or die ("Could not execute query: $q.".mysql_erro r());
    while($row=mysq l_fetch_array($ result)){
    $ID=$row["ID"];
    $xxx=$row["xxx"];
    $PhoneNo=$row["PhoneNo"];
    $District=$row["District"];
    $date=$row["date"];
    $vDescription=$ row["Descriptio n"];?>
    <tr class="row">

    </td>
    </tr>
    <?php }?>
    Hello,
    try this in if condition
    [PHP]$searchtext=$_p ost['search'];
    $q="SELECT * FROM 'xxx' WHERE 'xxx' LIKE '%$searchtext%' " ;
    [/PHP]

    Comment

    • hadi00
      New Member
      • Mar 2008
      • 9

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

      Comment

      Working...