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

  • SEARCH
  • Home
  • 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 8071809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:48:56+00:00 2026-06-05T13:48:56+00:00

I have a form that searches a MySQL database using PHP. Currently, when a

  • 0

I have a form that searches a MySQL database using PHP. Currently, when a user inputs a search into one of two fields, the entire contents of the database are displayed. Also, if the user leaves both fields blank, again, the entire contents of the database will be displayed.

However, if the user inputs random information into both of the fields, then the results page will be blank.

The assumed usage of this form is that the user can search for an article based on the article’s title, the article’s author or organization, or the article’s title and its author or organization by either filling out one or both of the fields.

What I’m trying to figure out is:

Why the results page keeps displaying all of the database contents.

and

How to ensure that the database is actually being queried rather than just being dumped by a coding error.

Code follows below:

search.php:

<div class="content">
    <form id="form1" name="form1" method="post" action="searchdb.php">
        <table width="100%" border="0" cellpadding="6">
            <tr>
              <td width="29%" align="right">Article Title:</td>
              <td width="71%" align="left"><input name="articletitle" type="text" id="articletitle" size="50" /></td>
            </tr>
            <tr>
              <td align="right">Author or Organization:</td>
              <td align="left"><input name="articleorganization" type="text" id="articleorganization" size="50" /></td>
            </tr>
        </table>


        <table width="100%" border="0" cellpadding="6">
          <tr>
            <td><input type="submit" name="submit" value="Submit" /></td>
          </tr>
        </table>
    </form>
</div>

searchdb.php

<?php

include('settings.php');
$query = "select * from articles";
$where = array();
if (!empty($_POST['articletitle'])) {
   $where[] = "articletitle LIKE '%".mysql_real_escape_string($_POST['articletitle'])."%'";
}
    if (!empty($_POST['articleorganization'])) {
       $where[] = "articleorganization LIKE  '%".mysql_real_escape_string($_POST['articleorganization'])."%'";
}
    if (!empty($where)) {
        $query .= " WHERE " . implode(" OR ", $where);
        $sql = mysql_query($query);
    } else {
        // No results
}
while ($row = mysql_fetch_array($sql)){
    echo '<br/> Article Title: '.$row['articletitle'];
    echo '<br/> Article Organization: '.$row['articleorganization'];
    echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
    echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
    echo '<td><a href="entry.php?id=' . $row['id'] . '">View Full Entry</a></td>';
    echo '<br/><br/>';
}

?>
  • 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-05T13:48:58+00:00Added an answer on June 5, 2026 at 1:48 pm

    Tested this, and it should do exactly what you want.

    $query = "select * from articles";
    $where = array();
    if (!empty($_POST['articletitle'])) {
       $where[] = "articletitle LIKE '%".mysql_real_escape_string($_POST['articletitle'])."%'";
    }
    if (!empty($_POST['articleorganization'])) {
       $where[] = "articleorganization LIKE '%".mysql_real_escape_string($_POST['articleorganization'])."%'";
    }
    if (!empty($where)) {
        $query .= " WHERE " . implode(" OR ", $where);
        $sql = mysql_query($query);
    } else {
        // No results
    }
    

    EDIT
    It appears your form is passing empty values, so instead of checking isset, check !empty. I have updated the code above.

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

Sidebar

Related Questions

I have written a simple mysql search form that searches between two dates. This
I'm creating a dictionary database using php and MySQL. The user can search for
I have a form that searches a mysql db for whatever the user is
I have a search form that searches a table in my database. The table
I currently have form that checks if a user has unsubmitted changes when they
I have a search form that the user can search for an Article content
I have a form that allows the user to perform a myriad of searches.
I have a simple form that searches through the 2000+ issues of a 3rd
I have a form that and after the user fills in this form, I
I have a form that collects information, one piece of which is a phone

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.