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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:19:45+00:00 2026-06-08T21:19:45+00:00

I have a script that will query my database and needs to be able

  • 0

I have a script that will query my database and needs to be able to query using one or all of four options. The options are start sku, end sku (this is one option as there has to be a range to search), created by and date created. I know I can do this using MANY if..elseif statements, but I have to think there is a better and easier way. Here is what I have for code so far:

$start =mysql_real_escape_string ($_POST['skuStart']);
$end = mysql_real_escape_string($_POST['skuEnd']);
$source = mysql_real_escape_string($_POST['source']);
$processDate = mysql_real_escape_string($_POST['processDate']);

if(!empty($start) && !empty($end) && empty($source) && empty($processDate)){
$result = $conn->query("Select * from inventory where sku >= $start and sku <= $end");

 } elseif (empty($start) && empty($end) && !empty($source) && empty($processDate)){
$result = $conn->query("Select * from inventory where created_by = '$source'");

 } elseif (empty($start) && empty($end) && empty($source) && !empty($processDate)) {
$result = $conn->query("Select * from inventory where date_process = '$processDate'");

} elseif(!empty($start) && !empty($end) && !empty($source) && empty($processDate)){
$result = $conn->query("Select * from inventory where sku >= $start and sku <= $end and created_by = '$source'");

} else {
$result = $conn->query("Select * from inventory where sku >= '$start' and sku <= '$end' and created_by = '$source' and date_process = '$processDate'");
}

while($row = $result->fetch_assoc())
    {

        $skuArray[$x] = $row['sku'];
        $isbnArray[$x] = $row['isbn13'];
        $qtyArray[$x] = $row['quantity'];
        $defectArray[$x] = $row['defect_id'];
        $sourceArray[$x] = $row['source_id'];
        $featureArray[$x] = $row['feature_id'];
        $locationArray[$x] = $row['location_id'];
        $processDateArray[$x] = $row['date_process'];
        $bookTypeArray[$x] = $row['book_type_id'];
        $createdByArray[$x] = $row['created_by'];
        $modifiedByArray[$x] = $row['modified_by'];

        $x++;

This very crude form is working, but is there a way to get the if elseif simplified?

NOTE: I know that I should be using PDO to prevent sql injection, but I have not fully learned that yet, so I am using this.

  • 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-08T21:19:47+00:00Added an answer on June 8, 2026 at 9:19 pm

    Setting aside the issues you’re already aware of – switching to PDO and using bound parameters – You could make your code a little clearer with the following:

    <?php
    
    $start          = mysql_real_escape_string ($_POST['skuStart']);
    $end            = mysql_real_escape_string($_POST['skuEnd']);
    $source         = mysql_real_escape_string($_POST['source']);
    $processDate    = mysql_real_escape_string($_POST['processDate']);
    $where          = array();
    
    if(!empty($start))
    {
        $where[] = 'sku >= ' . $start;
    }
    
    if(!empty($end))
    {
        $where[] = 'sku <= ' . $end;
    }
    
    if(!empty($source))
    {
        $where[] = 'created_by = \''.$source .'\'';
    }
    
    if(!empty($processDate))
    {
        $where[] = 'date_process = \''.$processDate .'\'';
    }
    
    $query = 'SELECT * FROM inventory';
    
    if(count($where))
    {
        $query .= ' WHERE ' . implode(' AND ', $where);
    }
    
    $result = $conn->query($query);
    
    while($row = $result->fetch_assoc())
        {
    
            $skuArray[$x] = $row['sku'];
            $isbnArray[$x] = $row['isbn13'];
            $qtyArray[$x] = $row['quantity'];
            $defectArray[$x] = $row['defect_id'];
            $sourceArray[$x] = $row['source_id'];
            $featureArray[$x] = $row['feature_id'];
            $locationArray[$x] = $row['location_id'];
            $processDateArray[$x] = $row['date_process'];
            $bookTypeArray[$x] = $row['book_type_id'];
            $createdByArray[$x] = $row['created_by'];
            $modifiedByArray[$x] = $row['modified_by'];
    
            $x++;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Perl script that will execute three applications. All of it have
I have a script that will get images from page, but how do I
I have a script that will convert a text file into a resource file,
Right now I have a script that will get the last five files in
I have a powershell script that will generate an email for users whose password
I have created a script that will upload files to a general uploads folder,
I have written a Python script that will generate a series of graphs and
I have a simple PHP script that will either serve up a streaming ASF
Does anyone have a script for logparser that will output a graph for daily
I'm writing a script that will ping my ip range. Here's what I have

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.