How to compare array with string?

3

The array $buscaMov[9] , contains the word "Internal", but the if only returns me false, in this case it will always stop at else . It should register in the table of purchases, but only register in sales.

if ($buscaMov[9] == "Interno"){

    $movInterno = mysql_query("INSERT INTO compras(descComp,precoComp,dataComp,setorComp) "
                            . "VALUES ($buscaMov[1],$buscaMov[7],$buscaMov[2],$buscaMov[8])");

}else{

    $movExterno = mysql_query("INSERT INTO vendas(setorVend,precoVend,formPagVend,dataVend,descVend)"
                           . " VALUES ('$buscaMov[8]','$buscaMov[7]','x-x-x','$buscaMov[2]','$buscaMov[1]')");

}
    
asked by anonymous 19.11.2015 / 18:19

1 answer

1

Make sure the string inside $buscaMov is the comparison table, set an ex pattern, use lowercase, and also remove spaces at the beginning and end of the string with trim()

Change: to:

if (trim(strtolower($buscaMov[9])) == "interno"){

To facilitate the identification of the error use the function mysql_error() .

$sql = sprintf("INSERT INTO compras(descComp,precoComp,dataComp,setorComp) VALUES ('%s', '%s', '%s', '%s')",
                $buscaMov[1],$buscaMov[7],$buscaMov[2],$buscaMov[8]);
$movInterno = mysql_query($sql) or die(mysql_error());
    
19.11.2015 / 18:32