Jump to content

Empty Variable in MYSQL string


_spaz

Recommended Posts

Is it possible to process MYSQL statement if one of the 3 variables used is empty?  I want the option to be able to leave a variable blank if nothing is entered in the search form.

 

Example:

Say I do a search by jobid and startdate but leave endate blank....(this is inputted in search form)

 

SELECT * FROM Media WHERE Jobid = '$_POST[option]' AND starttime >= '$_POST[startdate]%' AND endtime <= '$_POST[enddate]%' GROUP BY mediaFile";

Link to comment
https://forums.phpfreaks.com/topic/177599-empty-variable-in-mysql-string/
Share on other sites

(1.)

 

Always use mysql_real_escape_string:

$sql = sprintf("SELECT * FROM $tableName WHERE fieldName LIKE '%s'",mysql_real_escape_string($fieldName));

 

which is much safer than doing without:

SELECT * FROM Media WHERE Jobid = '$_POST[option]'

 

Without it you will be open to SQL Injection Attacks.

 

 

(2.)

 

You could simply do:

 

if(only box 1 completed) {

$sql = box 1 only

}

 

if(box 1 and 2 completed) {

$sql = box 1 and 2 only

}

 

if(all 3 boxes used) {

$sql = all 3

}

 

This will work but uses multiple IF statements. Someone will probably give you a more refined version - but like I say it would work fine.

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.