php – How to compare array with string?

Question:

The $buscaMov[9] array contains the word "Internal", but the if only returns false, in this case, it will always stop at the else . It should register in the purchases table, but it only registers 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]')");

}

Answer:

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

Change: to:

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

To facilitate error identification use the mysql_error() function.

$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());
Scroll to Top
AllEscort