INSERT PHP MYSQL does not insert

Asked

Viewed 1,770 times

-1

I have a simple problem with INSERT in the PHP with MYSQL , happens that it does not insert anything in the database , already put to report the errors but 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());

?>
  • 1

    Is there a mistake? You have the $con defenide?

  • Amigo posts his sql table and code but it is likely that some comma or field insertion is missing.

  • no error , this $con is my connection

  • Error gives, but you are not playing it on the screen. See answer.

2 answers

3


Basically it is inattention when seeing documentation or examples.

You’re using a function that serves you no purpose here: or die( mysql_error() );

In fact, mysql_error() will always be empty in this context as there are no commands from mysql being executed, therefore, there is no way to produce detectable errors with this function.

Now, to get errors from lib that you are actually wearing, which is the mysqli, you have a suitable function for getting errors, which is this one: mysqli_error($link).

Applied to your case:

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

And here you need to tidy up 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 ) );
  • Thanks for the help I will pay attention to these errors, but I gave a print in my query to the database and I checked that I was not entering because I had to insert in all the columns that exist in my table , but I didn’t understand much because in creating them I didn’t say that they should be mandatory

  • The first step is to actually display the errors, and from there it is quite easy to detect where it is failing.

-1

You are missing to tell mysqli_connect which database to use for this: mysqli_connect ($host, $user,$password, $bandodedice). Or in your query refer to the database to which you will use (Insert into bancodedados.tb_cadastro ...)

  • 1

    the function mysqli_select_db already does this

  • Ah, I hadn’t seen it. So I don’t see any reason for error. Make sure your query is working. Test her on a phpMyAdmin, or Heidisql...

Browser other questions tagged

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