Jump to content

[SOLVED] Query troubles


phatgreenbuds

Recommended Posts

Consider the following code:

[color=green][i]$ownerq = $_POST[Owner];
$statusq = $_POST[Status];
$typeq = $_POST[Type];

$query = "SELECT *
FROM crdmain
WHERE reqowner='$ownerq'
AND reqstatus='$statusq'
AND reqtype='$typeq'";[/i][/color]

The variable "$typeq" could be any of five different values selectable from a drop down.  This works fine as is, and of course assuming a specific value is chosen. The problem I am having is how to add an "Any" value that will return a query for all five options.  I thought I could use functions nested in "If" statements to jump between different query's but either it does not work or my syntax was wrong. Any ideas?
Link to comment
https://forums.phpfreaks.com/topic/31935-solved-query-troubles/
Share on other sites

Try something like this:

[code]
<?php
$ownerq = $_POST[Owner];
$statusq = $_POST[Status];
$typeq = ($_POST[Type]=='Any') : '' : "AND reqtype='{$_POST[Type]}'";
//this basically says that if $_POST[Type] is equal to the string "Any" then leave this whole thing empty,
//otherwise make it equal to the string "AND reqtype='the value of $_POST[Type]'"

$query = "SELECT * FROM crdmain WHERE reqowner='$ownerq' AND reqstatus='$statusq' $typeq";
//by default, the query is made using only reqowner and reqstatus, the variable $typeq comes from above,
//and can modify the query

?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/31935-solved-query-troubles/#findComment-148192
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.