Problems in using php Insert

Asked

Viewed 110 times

0

I would like to ask a question, I’m trying to make a simple Internet with php and mysql but I’m giving 3 errors, I’ve already found that are the connection, only I give the include in the connection (which is an external file) and even then the errors are there, please what am I doing wrong? This is the php file I’m making 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 file of connection

<?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();
}
?>
  • "and yet the errors are there" - but what are the errors? include the error messages in the question using the [Edit link].

  • The PDO and Mysqli do not communicate... in myqli_query() need to pass two arguments the first is the connection (Mysqli) and the second the query.

2 answers

1

A PDO connection does not work with the Mysqli library, so include is not right. The other problem is that it failed to pass the connection to the mysqli_query() that makes 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')");

1

You are mixing PDO with mysqli_query are separate things.

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

gives a look at the doc that I think facilitates php.net/manual/en/Pdo.connections.php and Secure.php.net/manual/en/class.mysqli.php

I made a simple object of connection to the database that I used a lot you can download in php classes https://www.phpclasses.org/package/9400-PHP-Execute-common-SQL-queries-using-MySQLi-extension.html

I hope I’ve helped.

Browser other questions tagged

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