Not bringing the data with multiple filters into a table mysqli

Asked

Viewed 20 times

-1

<?php
$query = "SELECT * from SALDO_GERAL";
$i = 0;
$selectedOptionCount = count($_POST['LOCAL']);
$selectedOptionCount2 = count($_POST['net']);
$selectedOption = "";
$selectedOption2 = "";
while ($i < $selectedOptionCount) {
$selectedOption = $selectedOption . "'" . $_POST['LOCAL'][$i] . "'"; 
$selectedOption2 = $selectedOption2 . "'" . $_POST['net'][$i] . "'";
if ($i < $selectedOptionCount - 1) {
$selectedOption = $selectedOption . ", ";
}
$i ++;
    }
$query = $query . " WHERE LOCAL = (" . $selectedOption . "  AND net = " . $selectedOption2 . ")";
$result = $db_handle->runQuery($query);
}
if (! empty($result)) {
foreach ($result as $key => $value) {
?>

  • Dude, for starters, why are you setting the query at the beginning of the code to only then concatenate to the WHERE clauses? and you pricisa pass more information about your code and about the error presented

1 answer

2


The error is on the line:

$query . " WHERE **LOCAL = (" . $selectedOption . "  AND net = " . $selectedOption2 . ")**";

remove the parentheses in the call, put it like this:

$query . " WHERE **LOCAL IN (" . $selectedOption . ")  AND net = '" . $selectedOption2 . "'**";

MYSQL: WHERE column_name IN (SELECT STATEMENT);

Simplify this code:

$query = "SELECT * from SALDO_GERAL";
$i = 0;
$selectedOptionCount = count($_POST['LOCAL']);
$selectedOptionCount2 = count($_POST['net']);
$selectedOption = "";
$selectedOption2 = "";
while ($i < $selectedOptionCount) {
$selectedOption = $selectedOption . "'" . $_POST['LOCAL'][$i] . "'"; 
$selectedOption2 = $selectedOption2 . "'" . $_POST['net'][$i] . "'";
if ($i < $selectedOptionCount - 1) {
$selectedOption = $selectedOption . ", ";
}
$i ++;
    }

Tip: Concatenate using ' .= ' instead of repeating the variable:

$query = "SELECT * from SALDO_GERAL";
$i = 0;

$selectedOptionCount = count($_POST['LOCAL']);
$selectedOptionCount2 = count($_POST['net']);
$selectedOption = "";
$selectedOption2 = "";

while ($i < $selectedOptionCount) {
    $selectedOption .= "'" . $_POST['LOCAL'][$i] . "'"; 
    $selectedOption2 .= "'" . $_POST['net'][$i] . "'";
    if ($i < $selectedOptionCount - 1) {
        $selectedOption .= ", ";
    }
    $i ++;
}

or uses php -> implode native function():

$query = "SELECT * from SALDO_GERAL";

if ( is_array( $_POST['LOCAL'] ) ){
    $selectedOption = "'" . implode("', '", $_POST['LOCAL']) . "'";
}
if ( is_array( $_POST['net'] ) ){
    $selectedOption2 = "'" . implode("', '", $_POST['net']) . "'";
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.