my code is giving "error in Count(): Parameter must be an array or an Object that Implements Countable in"

Asked

Viewed 520 times

-1

the two files that make connection

and that:

<?php
    include("classe/conexao.php"); 
    
    if(isset($_POST['confirmar'])){

        //1- Registro dos dados
        if(!isset($_SESSION))
            session_start();

            foreach($_POST as $chave=>$valor){
                $_SESSION[$chave] = $mysqli-> real_escape_string($valor);
            }

        //2- Validação dos dados 
            if(strlen($_SESSION['nome']) == 0 )
                $erro[] = "Preencha o Nome ";

            if(strlen($_SESSION['sobrenome']) == 0 )
            $erro[] = "Preencha o Sobrenome ";

            if(strlen($_SESSION['sexo']) == 0 )
            $erro[] = "Selecione o Sexo ";

            if(substr_count($_SESSION['email'],'@') !==1 ||substr_count($_SESSION['email'], '.') < 1 || substr_count($_SESSION['email'], '.') > 2)
            $erro[] = "Preencha o Email corretamente ";

            if(strlen($_SESSION['nivelacesso']) == 0 )
            $erro[] = "Preencha seu Nivel de Acesso ";

            if(strlen($_SESSION['senha']) < 8 || strlen($_SESSION['senha']) > 16 )
            $erro[] = " Preencha a senha correntamente ";

            if(strcmp($_SESSION['senha'], $_SESSION['rsenha'])!= 0 )
            $erro[] = "As senhas não batem ";


        //3- Inserção no Banco e redirecionamento

        if(count($erro)==0){
            $sql_code = "INSERT INTO usuario(
                nome,
                sobrenome,
                sexo,
                email,
                senha,
                nivelacesso
                )
                VALUES(
                '$_SESSION[nome]',
                '$_SESSION[sobrenome]',
                '$_SESSION[sexo]',
                '$_SESSION[email]',
                '$_SESSION[senha]',
                '$_SESSION[nivelacesso]'
                )";
            $confirma = $mysqli->query($sql_code) or die($mysqli->error);

            if($confirma){
                unset($_SESSION[nome],
                $_SESSION[sobrenome],
                $_SESSION[sexo],
                $_SESSION[email],
                $_SESSION[senha],
                $_SESSION[nivelacesso]);

                echo "<script> location.href='index.php?p=inicial'</script>";
            }else 
                $erro[] = $confirma;
        }
    }

?>

<h1>Cadastrar Usuário</h1>

<?php 

if(count($erro)> 0){ 
    echo "<div class='erro'>";
    foreach($erro as $valor) 
    echo "$valor <br>"; 
    echo  "</div>";
}
?> 

<a href="index.php?p=inicial">voltar</a>
<form action="index.php?p=cadastrar" method="post">

    <!-- nome -->
    <label for="nome">Nome</label>
    <input type="text" name="nome" id="nome" value="" require>
    <p class="espaco"></p>

    <!-- sobrenome -->
    <label for="sobrenome">Sobrenome</label>
    <input type="text" name="sobrenome" id="sobrenome" value="" require>
    <p class="espaco"></p>

    <!-- sexo -->
    <label for="sexo">Sexo</label>
    <select name="sexo" id="sexo">
        <option value="" >Selecione</option>
        <option value="1">Masculino</option>
        <option value="2">Feminino</option>
    </select>
    <p class="espaco"></p>

    <!-- email -->
    <label for="email">Email</label>
    <input type="text" name="email" id="email" value="" require>
    <p class="espaco"></p>

    <!-- nivelacesso -->
    <label for="nivelacesso">Nível De acesso</label>
    <select name="nivelacesso" id="nivelacesso">
        <option value="">Selecione</option>
        <option value="">basico</option>
        <option value="">admin</option>
        
    </select>
    <p class="espaco"></p>

    <!-- senha -->
    a senha deve ter entre 8 e 16 caracteres 
    <label for="senha">Senha</label>
    <input type="password" name="senha" id="senha" value="">
    <p class="espaco"></p>

    <!-- repita a senha -->
    <label for="rsenha">Repita a Senha</label>
    <input type="password" name="rsenha" id="rsenha" value="">
    <p class="espaco"></p>

    <input type="submit" value="Salvar" nome="confirmar">
</form>

with that:

<?php

$host ="localhost";
$user="root";
$pass="";
$banco="labrador";
$conexao=mysqli_connect($host, $user, $pass,$banco) or die(mysqli_error());
mysqli_select_db($conexao,$banco) or die(mysqli_error());

?>

I don’t know what’s going on that helps me with this code please?

1 answer

0


If you change the attribute nome (which I do not know) in

<input type="Submit" value="Save" nome="confirm">

for name

<input type="Submit" value="Save" name="confirm"> the error

count(): Parameter must be an array or an object that implements Countable in

going cease to exist.

Only that this error will be replaced by the error

Fatal error:  Uncaught Error: Call to a member function real_escape_string() on null in

That one error occurs because you instanced your connection string as $conexao and is using $mysqli

Therefore correct

$_SESSION[$chave] = $mysqli-> real_escape_string($valor);

for

$_SESSION[$chave] = $conexao-> real_escape_string($valor);

And for there to be a substitute

$confirma = $mysqli->query($sql_code) or die($mysqli->error);

for

$confirma = $conexao->query($sql_code) or die($conexao->error);

I don’t see why working with tantas sessions, as soon after the INSERT you destroy them unset($_SESSION[nome] .... and that this will cause you more errors (one for each unset

PHP Warning:  Use of undefined constant nome - assumed 'nome' (this will throw an Error in a future version of PHP) in

do according to the code below and if you need one or the other.

<?php
  $host ="localhost";
  $user="root";
  $pass="";
  $banco="labrador";
  $conexao=mysqli_connect($host, $user, $pass, $banco);

 if ($conexao->connect_errno)
   {
     die('Could not connect');
     exit();
   }
   
  $erro=[]; 

if(isset($_POST['confirmar'])){

         //1- Registro dos dados
        if(!isset($_SESSION)){
            session_start(); 

            foreach($_POST as $chave=>$valor){
                //variaveis dinamicas 
                $$chave = $conexao-> real_escape_string($valor);
            }
        }
 

        //2- Validação dos dados 
            if(strlen($nome) == 0 )
                $erro[] = "Preencha o Nome ";

            if(strlen($sobrenome) == 0 )
            $erro[] = "Preencha o Sobrenome ";

            if(strlen($sexo) == 0 )
            $erro[] = "Selecione o Sexo ";

            if(substr_count($email,'@') !==1 ||substr_count($email, '.') < 1 || substr_count($email, '.') > 2)
            $erro[] = "Preencha o Email corretamente ";

            if(strlen($nivelacesso) == 0 )
            $erro[] = "Preencha seu Nivel de Acesso ";

            if(strlen($senha) < 8 || strlen($senha) > 16 )
            $erro[] = " Preencha a senha correntamente ";

            if(strcmp($senha, $rsenha)!= 0 )
            $erro[] = "As senhas não batem ";


        //3- Inserção no Banco e redirecionamento

        if(count($erro)==0){
            $sql_code = "INSERT INTO usuario(
                nome,
                sobrenome,
                sexo,
                email,
                senha,
                nivelacesso
                )
                VALUES(
                '$nome',
                '$sobrenome',
                '$sexo',
                '$email',
                '$senha',
                '$nivelacesso'
                )";
            $confirma = $conexao->query($sql_code) or die($conexao->error);

            if($confirma){
                unset($nome,
                $sobrenome,
                $sexo,
                $email,
                $senha,
                $nivelacesso);

                echo "<script> location.href='index.php?p=inicial'</script>";
            }else{
                $erro[] = $confirma;
            }
        }


}
?>


<h1>Cadastrar Usuário</h1>

<?php 

if(count($erro)> 0){ 
    echo "<div class='erro'>";
    foreach($erro as $valor) 
    echo "$valor <br>"; 
    echo  "</div>";
}

?>

Dynamic variables

  • Notice: Undefined variable: error in C: xampp htdocs Complete Registration system contains.php on line 83 this error is giving in my code and do not know pq, could help,

  • @Hugolelis, what’s on line 83?

  • if(is_countable($error) && Count($error) > 0){

  • the variable $error is undefined, it is undefined and does not let me upload the code

  • @Hugolelis, The question(s) problem was/were solved(s) and you did not signal the answer as accepted and already amended with another question that is not the focus of this post. The correct one would be to make another post with this new situation. For this new situation declare the variable $error at the beginning of the code $error=[];

  • @Hugolelis, actually there are still more errors in your script besides this $error being as undefined. But it’s like I told you, the focus of the question is Parameter must be an array or an Object that Implements Countable in that was solved with the answer and even more so the Fatal error: Uncaught Error: Call to a Member Function real_escape_string() on null in was also given as a bonus.

  • @Hugolelis, why don’t you ask him why he’s working with so many women? It’s not the best way to work since you might for some reason miss the Sesssions.

  • I’m learning php now I have no idea why use so many Sessions

  • @Hugolelis, it is, so learn and do it right as the last of the answer.

  • Of course my dear colleague, I apologize for the delay of not responding fast, you could indicate me something to study in the correct way?

Show 5 more comments

Browser other questions tagged

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