Database connection error

1

I'm doing a project and this has a problem in the database part. I created a form to register on the site, but when you put all the data to register, this information is not passed to the database. Can anyone tell me what might be wrong?

form

            <div class="col-md-6 logo">
            Cadastre-se
            <form method="post" action="registra.php" id="cadastro">
                <br>
                <div class="form-group">
                    <input type="text" class="form-control" id="nome" name="nome" placeholder="Digite seu nome completo" required="requiored">
                </div>
                <br>
                <div class="form-group">
                    <input type="email" class="form-control" id="email" name="email" placeholder="Email" required="requiored">

                    <?php
                    if($erro_email){
                    echo '<font size="3px" color="#FF0000"> Esse email ja esta sendo usado em outra conta!</font>';
                }

                ?>
            </div>
            <br>
            <div class="form-group">
                <input type="password" class="form-control" id="senha" name="senha" placeholder="Crie uma senha" required="requiored">
            </div>


            <button type="submit" class="btn btn-default ">Cadastrar</button>
        </form>
        </div>

Connecting to the database

<?php

require_once('bancodedados.php');

 $usuario = $_POST['usuario'];
 $email = $_POST['email'];
 $senha = md5($_POST['senha']);

$objDB = new db();
$link = $objDB->conecta_mysql();

$email_existe = false;

$sql = "insert into usuarios(usuario, email, senha) values ('$usuario','$email','$senha')";

//verificar se o email ja existe
$sql = "select * from usuarios where email = '$email'";
if($resultado_id = mysqli_query($link, $sql)){

    $dados_usuario = mysqli_fetch_array($resultado_id);

    if (isset($dados_usuario['email'])) {
        $email_existe = true;
    }  
} else {
    echo ' erro ao tentar localizar o email';
}

    if($email_existe){

        $retorno_get ='';

        $retorno_get.="erro_email=1&";

        header('Location:cadastreentrar.php?'.$retorno_get);
die();
    }

//executar querry
if(mysqli_query($link, $sql)){
    header('Location:home.php');
} else {
    echo 'Não foi possivel registrar o usuario';
}

? >

    
asked by anonymous 14.07.2017 / 19:17

1 answer

0

You are not executing the insert query, only the select query. Let's go to the table test:

$sql = "insert into usuarios(usuario, email, senha) values ('$usuario','$email','$senha')";
//Criou a string do insert, chamada sql, e atribui o insert

$sql = "select * from usuarios where email = '$email'";
//Atribuiu o select para o sql (que continha o insert)

if($resultado_id = mysqli_query($link, $sql)){
//Executou o comando do sql (select)

Did you realize what you did? You created the command to insert the values into the database, but did not execute it, you overlaid another string on it.

To fix it, do it this way:

$sql = "select * from usuarios where email = '$email'";

if($resultado_id = mysqli_query($link, $sql)){ //Executa o select

    $sql2 = "insert into usuarios(usuario, email, senha) values ('$usuario','$email','$senha')";

    mysqli_query($link, $sql2); //Executa o insert
}

Tip: Your code is too complex to do things that are so simple, try to improve it and optimize it.

    
14.07.2017 / 19:24