0

PHP + HTML:

I have three list boxes the contents of which(when clicked) are placed into a variable in a sql SELECT string.

Something like this:

SELECT * FROM table where field1= '" & variable1 & "' AND field2 = '" & Variable2 &"' AND field3 = '" & Variable3 & "'"

This works well if all the list boxes are clicked BUT if I only click 2 or 1 of the list boxes nothing happens and I get a blank table.

I tried it with check boxes and get the same thing. The thing is I will be expanding the number of Lists to 12 but this will produce the same problem.

Very new to all of this so any suggestion much appreciated. Kind Regards jim

1
  • Can you post the PHP code that generates the query, and the HTML of the form? Commented Mar 31, 2014 at 12:46

6 Answers 6

1

When a checkbox is not checked it is not submitted to the server! Your PHP code probably depends on that while it shouldn't.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use this query with your variable names.

$query = "SELECT * FROM TABLENAME WHERE field1 LIKE '".$variable1."' AND field2 LIKE '".$variable2."' AND field3 LIKE '".$variable3."'";

Comments

1

Just check if variable exist before adding it to your sql
Your code should be something like that

if(isset($variable1)){
  $sql .= " AND field1 = '$variable1'";
}

Comments

0

only the checked checkboxes get posted. so it needs slightly different appraoch. You can acheive it like this- put a hidden input with the same name as the checkbox that might not be checked. I think it works so that if the checkbox isn't checked, the hidden input is still successful and sent to the server but if the checkbox is checked it will override the hidden input before it. This way you don't have to keep track of which values in the posted data were expected to come from checkboxes.

<form>
  <input type='hidden' id='testName' value='0' name='carga1'>
  <input type='checkbox' id='testNameHidden' value='1' name='carga1'>

</form>

Before submitting the form , disabled the hidden field based on the checked condition.

<script>
if(document.getElementById("testName").checked){
  document.getElementById('testNameHidden').disabled = true;
}
</script>

I personally think its the easiest approach for this.

Comments

0

The listbox should have the first entry as empty so the use doesn't have to select anything. Your php code should use

$fld1 = isset($_POST['field1']) ? $_POST['field1'] : "";

Then build your sql statement.

$sql = "SELECT * FROM table ";
$where ="";
if( $fld1 != "" ) $whr .= 'field1 = '.$fld1;
if( $fld2 != "" {
  if( $whr != "" ) $whr += " AND ";
  $whr .= 'field2='.$fld2;
}

Comments

0

I suggest building an array of SQL conditions, while only adding a condition if something in the respective list box has been selected. That would result in something like this:

$conds = array(1);
for ($i = 1; $i <= 3; $i++)
    if (isset($_POST['Variable' . $i]))
        $conds[] = "field" . $i . " = '" . $_POST['Variable' . $i] . "'";
$query = "SELECT * FROM table WHERE " . implode(" AND ", $conds);

Of course I don't know the exact identifiers of your variables, so, take this as an orientation.

Edit: Changed code slightly as proposed by Doge in the comments.

6 Comments

In fact, there is a case where a variable might be empty ;) Use isset().
Your WHERE trick still fails if $conds is empty. You need to initialize $conds as array("1").
@blue: empty($var) is a shortcut for (!isset($var) || $var == null)
@Callidior I think blue means the case where $var has an empty value like "". I think isset or array_key_exists is correct here.
@Doge: Ah, okay. I assumed that the question owner would also like to exclude ignore empty variables; but I can't know that. Sorry, blue, for that misunderstanding. I will change that code again to use isset().
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.