INSERT PHP MYSQL does not insert [closed]

-1

I have a simple problem with INSERT on PHP with MYSQL , it happens that it does not insert anything in the database, I already put it to report the errors but it does not work, I'm using vertrigo server

The code is this:

<?php
 include '../config/config.inc.php';

ini_set('display_errors',1);
ini_set('display_startup_erros',1);
error_reporting(E_ALL);

 //$usuario = $_POST['fu_usuario'];
 //$senha = $_POST['fu_senha'];
 //$email = $_POST['fu_email'];

 $sql = "INSERT INTO tb_cadastro (username,senha,email) VALUES ('teste','123','[email protected]')" ;
 $res = mysqli_query($con,$sql) or die (mysql_error());
 $num = mysqli_affected_rows($con);


   if($num > 0){
     echo "Usuario cadastrado com sucesso";
  }else{
    echo "Usuario não cadastrado";
  }


   mysqli_close($con);
?>

Connection code:

<?php 

   $con = mysqli_connect("localhost","root","vertrigo")  or die 
("Erro de conexao ao BD --> ". mysql_error());
   mysqli_select_db($con,"dbsistema") or die("Erro de selecao do bd ". mysql_error());

?>
    
asked by anonymous 25.06.2016 / 20:34

2 answers

3

It's basically inattentiveness to see the documentation or examples.

You are using a function that is of no use to you here: or die( mysql_error() );

In fact, mysql_error() will always be empty in this context, since there are no mysql commands being executed, so there are no detectable errors with this function.

Now, to get lib errors you're actually using, which is mysqli , you have a proper error-handling function here: mysqli_error($link) .

Applied to your case:

$res = mysqli_query( $con, $sql ) or die( mysqli_error( $con ) );

And here you need to fix too:

$con = mysqli_connect( … )  or die( 'Erro de conexao ao BD: '.mysqli_error( $con ) );
mysqli_select_db( … ) or die( 'Erro de selecao do BD:'.mysqli_error( $con ) );
    
26.06.2016 / 01:08
-1

You are missing out on telling mysqli_connect which database you will use for this: mysqli_connect ($ host, $ user, $ password, $ banded). Or in your query refer to the database you will use (insert into bancodedados.tb_cadastro ...)

    
25.06.2016 / 20:41