How to compare array to string?

Asked

Viewed 484 times

3

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

}
  • 1

    Are you sure $buscaMov[9] does it even contain "Internal"? Before the if puts a var_dump($buscaMov); and put the result just to ensure...

  • 1

    It has yes, look there: [9]=> string(7) "Internal"

  • Do $teste = $buscaMov[9] == "Interno"; var_dump($teste);

  • In thesis it is to work like this, see an example on IDEONE.

  • 1

    Comes from a csv these values? could put where the array is initialized?

  • If you’re working, what you’re not, is the first Internet if it’s equal to internal ...

  • 1

    Strings(texts) must be in single quotes. Is it working again from scratch? rs

  • Exact @rray, the problem was this same

  • What was the problem? I changed my answer, see if it solves the problem.

  • 1

    The problem was the simple quotes missing in the first Insert ''

Show 5 more comments

1 answer

1

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

Change: to:

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

To facilitate error identification 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());
  • This does not seem to be the problem since the strings are equal by what the AP says.

  • 1

    +1 But if the strings hit, in thesis it is to work the way it is right: https://ideone.com/3XyKCR

Browser other questions tagged

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