Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9090477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:22:48+00:00 2026-06-16T22:22:48+00:00

I have a html form tat my user can use to search through a

  • 0

I have a html form tat my user can use to search through a table in my MYSQL database.
By default if you just hit go it will display the entire table, however I would like them to be able select certain fields and my php form to search via the fields that are filled in.

I seem to be unable to find a way of doing this without writing a seperate query for all 11 inputs in the different combinations they could be entered in, which comes out at a total of 76 queries..

If anyone has a way to simplify this I would love any advice.

I have tried just running a query with the AND operator but that doesnt work as some variables can be left empty and that will return no result, not sure if that is what is upposed to happen, but that is what is happening.

my html and php:

http://jsbin.com/oquwid/1/edit

PHP

$sql = "SELECT * FROM  ".$tbl_name." 
WHERE fname='".$fname."' 
and lname='".$lname."' 
and city='".$city."'
and phone='".$pohne."'
and interest_inet='".$internet."'
and interest_tv='".$television."'
and interest_voice='".$voice."'
and submission_ip='".$ip."'
and inquiry_handled='".$handled."'";
$result = mysql_query($sql);

echo "<table border='1'>";
echo "<tr>";
$i = 0;

while ($i < mysql_num_fields($result))
{
    $meta = mysql_fetch_field($result, $i);   
    echo "<th>".$meta->name."</th>";
    $i++;
}

while ($row = mysql_fetch_row($result))
{
    echo '<tr>';

foreach($row as $item)
{
    echo "<td>".$item."</td>";
}

echo '</tr>';
echo $row;
}

echo "</table>";
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:22:49+00:00Added an answer on June 16, 2026 at 10:22 pm

    The reason you’re query isn’t working if a value is not filled in, is probably because the query results in this (given first name is empty)

    SELECT * FROM $tbl_name WHERE fname=''
    

    And there probably isn’t a user having no first name.

    Further, you considered adding a flag per requested info, and on base of that either add or remove the needed part to the select part of the query ?

    For example,

    $sql = "SELECT * FROM $tbl_name WHERE "; 
    $queryChanged = false;
    if (isset($fname)){
        if (!empty($fname)){
            $sql .= "fname='$fname' ";
            $queryChanged=true;
        }
    }
    if (isset($lname)){
        if (!empty($lname)){
            $sql .= ($queryChanged) ?  " AND lname='$lname'" : "lname='$lname'";
            $queryChanged = true;
        }
    }
    ... //Continue the logic
    

    I’d recommend you to read this post about select * as well as this about user input and how to handle it

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML form that a user can add an arbitrary amount of
I have a HTML form, which includes radio buttons that the user can select
I have an HTML form which, when submitted by the user, will call a
i have html form with ENCTYPE=multipart/form-data .It just contains the textbox of type 'file'
I have an HTML form containing some textboxes and two jqGrids. The user selects
I have html form and I want to add validation on user input -
I have an html form that allows the user to browse for a file
I have an html form consisting of just radio buttons. The 2nd line of
I have HTML form with input fields. Some of inputs can be empty, i.e.
I have a html form. Users can fill in the form in both english

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.