Counting array erroneously

Asked

Viewed 45 times

-2

I’m trying to make an if structure that depends on the number of content in an array and apparently is counting wrong, because regardless of how much content I select it redirects me to the page I asked for in one 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>";
            }
        }
  • always falls in Is? ever tried to give a var_dump($linhas) ?

  • falls in if ever, which would be a var_dump ?

  • When you do $array[] = 'alguma coisa'; it adds a new element. It seems that the mysqli_fetch()

  • the problem is that in mysqli_fetch you can not do $lines += mysqli_fetch_array($result), so it will always create a new $lines and not add more lines in the same array, the structure is a foreach... I tried with fetch and it did not give mt right

  • What do you want your code to do? more than one Cpf can be sent?

  • No, exactly the opposite first it counts how many cpfs are being selected, if it is more than one or 0 he of the error if it is only 1 it passes to another window in which I will put the data referring to this Cpf inside some textbox

Show 1 more comment

2 answers

3


If you need to know how many records were found in the query simply use the function mysqli_num_rows(). If only a 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 executes a query and can return a false if the query fails or a Resource if it succeeds. If the foreach is left over there $linhas will always have only one element, which makes you fall in if ever.

foreach ($_POST['selecionado'] as $cpf){
  //linhas omitidas
  $result = mysqli_query($strcon,$sql);
  $linhas[] += $result;
  • ai that is, the selected is a checkbox array in which each one stores a Cpf value, that your answer would take only one Cpf

  • @Caiovieira this information of the checkbox had no question and the commenting implied that only one Cpf was sent. In this case I would do a page by part with the textbox and give include instead of redirect.

  • turned out to work here rray, the error was up there in the action that was action="cadastro.php", I removed it and it worked, but now I do not know how I send the results of my array to the other page, another form inside the textbox of it...

-1

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

array_push($lines, $result);

Browser other questions tagged

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