Problems in insert using php

0

I would like to ask a question, I am trying to make a simple insert with php and mysql but I am giving 3 errors, which I have already discovered are the connection, only I give the include in the connection (which is an external file) and even though the mistakes are there, please what am I doing wrong? This is the php file that I'm doing the insert

<?php
 include "conexao.php";     
 if(@$_GET["go"] == "cadastrar"){
    $nome = $_POST["nome"];
    $email = $_POST["email"];
    $usuario = $_POST["usuario"];
    $senha = $_POST["senha"];

    if(empty($nome)){
        echo "<script>window.alert('Devem ser preencidos todos os campos para realizar o cadastro!'); history.back();</script>";
    }
    elseif(empty($email)){
        echo "<script>window.alert('Devem ser preencidos todos os campos para realizar o cadastro!'); history.back();</script>";
    }
    elseif(empty($usuario)){
        echo "<script>window.alert('Devem ser preencidos todos os campos para realizar o cadastro!'); history.back();</script>";
    }
    elseif(empty($senha)){
        echo "<script>window.alert('Devem ser preencidos todos os campos para realizar o cadastro!'); history.back();</script>";
    }
    else{
        $query = mysqli_num_rows(mysqli_query($con,"SELECT * FROM CADASTRO WHERE USUARIO = '$usuario'"));
        if($query == 1){
            echo "<script>window.alert('Usuário já existente!'); history.back();</script>";
        }else{
            mysqli_query("insert into cadastro(nome,email,usuario,senha) values('$nome','$email','$usuario','$senha')");
            //echo "<script>window.alert('Usuário cadastrado com sucesso!');</script>";
            //echo "<meta http-equiv = 'refresh' content = '0,url = 'principal.php'>";
        }
    }

}
?>

And below the connection file

<?php
$servidor = "localhost";
$banco = "bancoteste";
$usuario = "root";
$senha = "";
try {
    $con = new PDO("mysql:host=$servidor;dbname=$banco", $usuario, $senha);
    // set the PDO error mode to exception
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
    echo "Não foi possível a conexão com o servidor de dados! Erro: " . $e->getMessage();
}
?>
    
asked by anonymous 11.07.2017 / 17:05

2 answers

1

A PDO connection does not work with the MySQLi library, so the include is not right. The other problem is that you failed to pass the connection to the mysqli_query() doing the insert.

Your connection file should look like this:

$con = mysqli_connect($servidor, $usuario, $senha, $banco);

Change:

mysqli_query("insert into cadastro(nome,email,usuario,senha)  values('$nome','$email','$usuario','$senha')");

To:

mysqli_query($con, "insert into cadastro(nome,email,usuario,senha) values('$nome','$email','$usuario','$senha')");
    
11.07.2017 / 17:21
1

You are mixing PDO with mysqli_query are different things.

To use PDO your query that is thus mysqli_query($con,"SELECT * FROM CADASTRO WHERE USUARIO = '$usuario'") would have to be foreach($con->query("SELECT * FROM CADASTRO WHERE USUARIO = '$usuario'") as $row){}

take a look at the doc that I think facilitates php.net/manual/en/pdo.connections.php and secure.php.net/manual/en_us/class.mysqli.php

I made a simple connection object with the bank that I used a lot you can download in php classes link

I hope I have helped.

    
11.07.2017 / 17:21