Counting array erroneously [closed]

-2

I'm trying to make an if structure that depends on the number of content in an array and apparently it's counting wrong, because regardless of how much content I select it redirects me to the page I've requested in a condition:

else if(isset($_POST['alterar'])){
            foreach ($_POST['selecionado'] as $cpf){
                $sql = "SELECT * FROM tbl_usuario WHERE cpf = '$cpf'";
                $result = mysqli_query($strcon,$sql) or die("<script type='text/javascript'>alert('Erro no acesso aos dados');</script>");
                $linhas[] += $result;
            }
            if(count($linhas) == 1){
                echo "<script type='text/javascript'>window.location='cadastro.php';</script>";
            }
            else{
                echo "<script type='text/javascript'>alert('Escolha uma linha apenas');</script>";
            }
        }
    
asked by anonymous 25.11.2016 / 19:54

2 answers

3

If you need to know how many records were found in the simplify query, use the mysqli_num_rows() function. If only one cpf form sent by the form removes that foreach

else if(isset($_POST['alterar'])){

    $sql = "SELECT * FROM tbl_usuario WHERE cpf = '{$_POST['selecionado']}'";
    $result = mysqli_query($strcon,$sql) or die("<script type='text/javascript'>alert('Erro no acesso aos dados');</script>");
    $linhas = mysqli_num_rows($result);
    if($linhas == 1){
        echo "<script type='text/javascript'>window.location='cadastro.php';</script>";
    }else{
        echo "<script type='text/javascript'>alert('Escolha uma linha apenas');</script>";
    }
}

The code below performs a query and may return a false if the query fails or a resource succeeds. If foreach is left there $linhas will always have only one element, which will always drop if.

foreach ($_POST['selecionado'] as $cpf){
  //linhas omitidas
  $result = mysqli_query($strcon,$sql);
  $linhas[] += $result;
    
25.11.2016 / 20:13
-1

Attempts to initialize the array before the foreach and insert the element into the array by push method

array_push ($ rows, $ result);

    
25.11.2016 / 20:12