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 6844669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:24:24+00:00 2026-05-27T00:24:24+00:00

I need some help on how to set up this page. Let’s say I

  • 0

I need some help on how to set up this page. Let’s say I have a mysql table of 5 or so fields of data (i.e. id, firstname, lastname, favorite color, favorite number). I would like to give the user the ability to search the table, so I create a “Search” page.

On the page, I’ll have text fields for ID, lastname and firstname. From here the user would insert data to search by. I guess what I’m confused about is how I can write a script that takes in different search criteria from the text fields and ignore the fields that are left blank. For example, if a user only puts in a first or last name, then all the entries with that same first/last name will be shown. If they only insert the id (would be unique), then all the content for that user will be shown. If they put in both a last name and a last name, then all the data for entries having that first and last name will appear.

I suppose this is basic but I’m new to this so any help on how to set this up would be greatly appreciated.

Code Update:

<?php
//Connect and select database

//if user wants to search
if(isset($_POST['submit']))
{

$sqlConditions = array();

if(isset($_POST['id'])){
$id = filter_var($_POST['id'], FILTER_VALIDATE_INT);
$sqlConditions[] = 'id = ' . $id;
} else {
$id = 0;
}

if(isset($_POST['firstname'])){
$firstName = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
$sqlConditions[] = 'firstname = ' . $firstName;
} else {
$firstName = '';
}

if(isset($_POST['lastname'])){
$lastName = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);
$sqlConditions[] = 'lastname = ' . $lastName;
} else {
$lastName = '';
}

$query = 'SELECT * FROM students WHERE ' . join (' AND ', $sqlConditions);

//print query
while ($row = $query->fetch_row())
{
  print "LastName = " . $row[0] . " FirstName = " . $row[1].
"Favorite Color = " . $row[2] . " Favorite Number = " . $row[3] . "<br /><br />\n";
}

}
?>


<html>
<body>
<form method = "POST" action = "<?php echo $_SERVER['PHP_SELF']; ?>">
<p>ID:<input type = "id" id="id" name="id" size="20" maxlength="40" /></p>
<p>First Name:<input type = "firstname" id="firstname" name="firstname" size="20" maxlength="40" /></p>
<p>Last Name:<input type = "lastname" id="lastname" name="lastname" size="20"    maxlength="40" /></p>
<input type="submit" id="submit" name ="submit" value="Search" />

</form>
</body>
</html>
  • 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-05-27T00:24:24+00:00Added an answer on May 27, 2026 at 12:24 am

    You may try the following approach:

    Step 1: get data from POST, validate it and compose the future sql conditions

    $sqlConditions = array();
    
    if(isset($_POST['Id'])){
      $id = filter_var($_POST['Id'], FILTER_VALIDATE_INT);
      $sqlConditions[] = 'Id = ' . $id;
    } else {
      $id = 0;
    }
    
    if(isset($_POST['FirstName'])){
      $firstName = filter_var($_POST['FirstName'], FILTER_SANITIZE_STRING);
      $sqlConditions[] = 'FirstName = ' . $firstName;
    } else {
      $firstName = '';
    }
    
    if(isset($_POST['LastName'])){
      $lastName = filter_var($_POST['LastName'], FILTER_SANITIZE_STRING);
      $sqlConditions[] = 'LastName = ' . $lastName;
    } else {
      $lastName = '';
    }
    

    Step 2: Compose the query

    $query = 'SELECT * FROM your_table WHERE ' . join (' AND ', $sqlConditions);
    

    Of course you will need to use mysql_real_escape_string or some modern approach as PDO to make sure you avoid SQL injection.

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

Sidebar

Related Questions

Need some help to solve this. I have a gridview and inside the gridview
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
ASP.NET 4.0 Need some help with this vexing HTTP POST problem - I have
I need some help with a CSS layout. It is set up like this:
Okay, I am stumped on this one and need some quick help. I have
Need some help about with Memcache. I have created a class and want to
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help with this problem in implementing with XSLT, I had already implemented
Need some help gathering thoughts on this issue. Our team is moving ahead with
I need some help from the shell-script gurus out there. I have a .txt

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.