Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Please, Help me create an SQL Query

Featured Replies

I have this form:

 

form_view.png

 

It is a HTML form that allows the user to select ONE OR MORE options in each box.

I have the user's choices posting to a php document that will then run an SQL query to return the firearms that match their criteria.

 

I have all the user's criteria assigned to PHP variables and ready to plug into an SQL Query. But that's where I need some help!

 

It needs to work as such:

 

SELECT *(all fields)

FROM firearms (table)

WHERE action= bolt OR lever

AND make= tikka OR weatherby OR gamo

AND calibre= 222 OR 270 OR 308 OR 338

AND stock= wood OR synthetic

AND barrel= stainless steel

 

Obviously this is not the correct syntax, and the WHERE criteria will change as users select different choices.

 

Can anyone help me to create the correct query that will satisfy the user's changing criteria?

 

Here is a snippet of the PHP I've been playing with showing my $variable arrays:

$option_action = $_POST['option_action'];
$option_make = $_POST['option_make'];
$option_calibre = $_POST['option_calibre'];
$option_stock = $_POST['option_stock'];
$option_barrel = $_POST['option_barrel'];

$query  = "SELECT * 
	   FROM firearms
	   where make 
               IN ('$option_make[0]', '$option_make[1]', '$option_make[2]', '$option_make[3]', '$option_make[4]')
	   ;

Not sure how your fields are in the database ... but here's a quickie to help you:

 

//suppose you have a field with multiple selection options named FIELD here's a quick way to build the where statement for it:
//$_POST['field'] will always be an array

$field = $_POST['field'];

//first check if the array has more than 1 element
if (count($field) > 1)
{
  $where_field = ' AND field IN('.implode(',',$field).') '; //see php.net for implode
}
else		//array has only 1 element so no need for commas separating the fields
{
  $where_field = " AND field = ' ".$field[0]." '  ";
}

 

After you check all the fields you can build your sql like this:

SELECT ........
FROM ......
WHERE 1=1		   //dummy where so you can have all the other WHERES using AND
$where_field1
$where_field2
etc

Hope this makes sense and helps. Good luck ;)

  • Author

I didn't manage to get this working completely.

 

I edited your ELSE statement to this to get it working:

$where_option_action = " AND action = '$option_action[0]' ";

 

But I still can't seem to get the IF statement working.

I broke it into two lines to help me understand it:

$temp = implode(',',$option_action);
$where_option_action = " AND action IN ('$temp') ";

 

I cannot work out how to get the $temp variable to become read as a variable.

 

The SQL IN command requires formatting like this: (' criteria ') to work, so I need two more 's around the $temp, right?

 

Sorry if I'm on the wrong track, I'm new to this stuff.

 

Jarrad

I cannot work out how to get the $temp variable to become read as a variable.

2 ways you can do that:

- use double quotes:

$var = " here is another $variable";
//this will not work
$var = 'here is another $variable';

- concatenate (use the dot . ):

$variable = "ABCD";
$var = "this is a string ' ".$variable. " ' blah blah";

echo $var; will output "this is a string 'ABCD' blah blah"

 

The SQL IN command requires formatting like this: (' criteria ') to work, so I need two more 's around the $temp, right?

 

Yes, you should use double quotes for the sql string and single quotes for the $temp.

  • Author

Thankyou for your reply. I'll revisit this today and see if I can get it working properly.

 

Much appreciated,

Jarrad

  • Author

Attached is the code showing how I have got it to work with the first of 5 option fields:

 

$option_action = $_POST['option_action'];
$option_make = $_POST['option_make'];
$option_calibre = $_POST['option_calibre'];
$option_stock = $_POST['option_stock'];
$option_barrel = $_POST['option_barrel'];

if (count($option_action) > 1)
{
  $temp = implode("', '",$option_action);
  $where_option_action = " AND action IN ('".$temp."')";
}
else
{
  $where_option_action = " AND action = '$option_action[0]' ";
}

$query = "SELECT * 
	  FROM firearms
	  WHERE 1=1
	  $where_option_action
	 ";

 

I will replicate this for the other option_fields and try to optimize it into a function (If I can! :) )

 

Thanks again for your help Mihai, its great to see people still have time for the 'newbies'! :D

Regards,

Jarrad

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.