blank fields in form still return results

Discussion in 'MySQL' started by wool01, Mar 8, 2010.

  1. #1
    Hi to all! I need help in my mysql database query. I have a simple form and at least 3-4 fields to enter some text to query. I don't have problems in querying but if i don't put any text to query or simply all my fields are blank, when i press the submit button, all my data in database are displayed or queried. Can someone help me how to fix this?
    Thanks in advance..
     
    wool01, Mar 8, 2010 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    can you post your query?? i think its the condition.
     
    bartolay13, Mar 8, 2010 IP
  3. wool01

    wool01 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    OK here is my code...
    <?php
    //code to connect db here
    include 'connectdb.php';

    //get values from form
    $name=$_POST['name'];
    $add=$_POST['add'];
    $phone=$_POST['phone'];
    $status=$_POST['status'];
    $customer_name=$_POST['customer_name'];

    //query to database
    $query="SELECT * FROM $tbl_name WHERE name LIKE '%".$name."%' AND add LIKE '%".$add."%' AND phone LIKE '%".$phone."%' AND status LIKE '%".$status."%' AND customer_name LIKE '%".$customer_name."%' ORDER BY name asc";
    $result=mysql_query($query);
    $num=mysql_numrows($result);
    mysql_close();

    // Start looping rows in mysql database.
    $i=0;
    while ($i < $num) {

    $name1=mysql_result($result,$i,"name");
    $add1=mysql_result($result,$i,"add");
    $phone1=mysql_result($result,$i,"phone");
    $status1=mysql_result($result,$i,"status");
    $customer_name1=mysql_result($result,$i,"customer_name");
    ?>

    <tr>
    <td><font face="Arial, Helvetica, sans-serif"><? echo $name1; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><? echo $add1; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><? echo $phone1; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><? echo $status1; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><? echo $customer_name1; ?></font></td>
    </tr>
    <?php
    $i++;
    }
    ?>
     
    wool01, Mar 9, 2010 IP