Error inserting data into BD with PHP

Asked

Viewed 63 times

-1

When trying to enter data into the BD, displays the following message:

Fatal error: Uncaught Error: Class 'usuario' not found in C: xampp htdocs CRUD interface cadastroUsuario.php:11 Stack trace: #0 {main} thrown in C: xampp htdocs CRUD interface cadastroUsuario.php on line 11

Form:

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>Cadastro de Usuário</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>
            <form  action="../interface/cadastroUsuario.php" method="post" >
                <fieldset>
                    <legend>Cadastro de Usuário</legend>
                    Nome:<br>
                    <input type="text" name="nome" required><br>
                    E-mail:<br>
                    <input type="email" name="email" required><br>
                    Senha:<br>
                    <input type="password" name="senha1" required><br>
                    Confirmar Senha:<br>
                    <input type="password" name="senha2" required><br>
                    <input type="submit" value="Cadastrar"><br>
                    <a href="../index.html">Voltar</a>
                </fieldset>
            </form>

        </div>
    </body>
</html>

interface receiving form information:

<?php

$nome = $_POST['nome'];
$email = $_POST['email'];
$senha1 = $_POST['senha1'];
$senha2 = $_POST['senha2'];

if($senha1===$senha2){
    $senha = $senha1;
    //require_once '../classe/usuario.php';
    $obj = new usuario();
    $obj->inserir($nome, $email, $senha);
}else{
    echo "erro.";
}
interface cadastroUsuario {
    public function inserir($nome, $email, $senha);


}

Data insertion class in BD:

<?php


class usuario implements cadastroUsuario{


    public function inserir($nome, $email, $senha) {
        //estebelecendo conexão com banco de dados
        $conexao = new conexao();
        $conn = $conexao->conectar();
        //instrução mysql para inserção de dados na tabela usuário do BD.
        $sql = "insert into usuario (nome, email, senha) values('$nome','$email','$senha');";
        if(mysqli_query($conn, $sql)){

            echo "Dados submetido com sucesso!";
        }else{
         echo 'Erro ao submeter os dados.';
        }

        }
    public function validaUsuario($dados){

        $conexao = new conexao();
        $conexao->conectar();
        $conn = $conexao->conectar();

        $sql = "select * from usuario where email= '$dados[0]' and senha= '$dados[1]';";
        $resultado = mysqli_query($conn, $sql);
        //verificar se existe algum resultado na consulta
        if(mysqli_affected_rows($resultado)){

        }else{
            return 0;
        }

    } 
    }



?>

Class that performs BD connection:

<?php


class conexao {
    private $host="localhost";
    private $usuario="root";
    private $senha="";
    private $bd="sistema";

    function getHost() {
        return $this->host;
    }

    function getUsuario() {
        return $this->usuario;
    }

    function getSenha() {
        return $this->senha;
    }

    function getBd() {
        return $this->bd;
    }

    function setHost($host) {
        $this->host = $host;
    }

    function setUsuario($usuario) {
        $this->usuario = $usuario;
    }

    function setSenha($senha) {
        $this->senha = $senha;
    }

    function setBd($bd) {
        $this->bd = $bd;
    }


    public function conectar(){
        $con=mysqli_connect($this->host, $this->usuario, $this->senha, $this->bd);
        return $con;

    }
    public function desconectar(){
        $con=mysqli_connect($this->host, $this->usuario, $this->senha, $this->bd);
        return mysqli_close($con);



    }
}
  • 1

    Apparently you didn’t include the user class in cadastroUsuario, but at the same time implemented the interface in that file, which will be required in the class file. Basically your code is quite confused. Remember that for the class to exist in a scope, you need to include it

  • How could I make the code more legit in this case?

2 answers

1

Try separating the classes, interfaces and files that receive form data into separate files. Ex:

Archive: cadastroUsuario.php

interface cadastroUsuario {
    //codigo da interface
}

user file.php

require_once "cadastroUsuario.php"; //faz o include da interface

class usuario implements cadastroUsuario{
    //métodos da classe usuario
    //implementações da interface cadastroUsuario
}

File validationUsuario.php

require_once "usuario.php";

$nome = $_POST['nome'];
$email = $_POST['email'];
$senha1 = $_POST['senha1'];
$senha2 = $_POST['senha2'];

if($senha1===$senha2){
    $senha = $senha1;
    $obj = new usuario();
    $obj->inserir($nome, $email, $senha);
}else{
    echo "erro.";
}

Finally, in your form, change the action to the file "validaUsuario.php"

<form action="validaUsuario.php" .... >
    ...
</form>

0

The error was in the user.php file

public function inserir($nome, $email, $senha) {
        //estebelecendo conexão com banco de dados

//I forgot to call through require the connection.php

        $conexao = new conexao();
        $conn = $conexao->conectar();
        //instrução mysql para inserção de dados na tabela usuário do BD.
        $sql = "insert into usuario (nome, email, senha) values('$nome','$email','$senha');";
        if(mysqli_query($conn, $sql)){

            echo "Dados submetido com sucesso!";
        }else{
         echo 'Erro ao submeter os dados.';
        }

        }
    public function validaUsuario($dados){

        $conexao = new conexao();
        $conexao->conectar();
        $conn = $conexao->conectar();

        $sql = "select * from usuario where email= '$dados[0]' and senha= '$dados[1]';";
        $resultado = mysqli_query($conn, $sql);
        //verificar se existe algum resultado na consulta
        if(mysqli_affected_rows($resultado)){

        }else{
            return 0;
        }

    } 
    }



?>

Browser other questions tagged

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