Database does not save data in php, although no error returns

Asked

Viewed 268 times

0

Hello :) I am starting learning with PHP, trying to insert data from the database. Although no error returns, the data is not saved. Here is the code:

    <?php

    $con=mysqli_connect("localhost","mazzu","");
    mysqli_select_db($con,"aulasphp");

    $nome="Matheus";
    $username="mazzu";
    $email="[email protected]";
    $senha="123";
    $tel=9999-99999;
    $status="ok";
    $obs="ok";


    $insere="insert into tb_cadastro values (NULL ,'$nome','$username',$email,'$senha','$tel','$status','$obs')";
    $res=mysqli_query($con,$insere);




    mysqli_close($con);

?>



<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>Aula 30</title>
    </head>

    <body>
    </body>
</html>
  • See if there are any errors: $res=mysqli_query($con,$insere) or die(mysqli_error($con);

  • @rray correcting: die(mysqli_error($con));

  • A good practice is to inform the fields after the table tb_cadastro(coluna1,coluna2....)

  • $email is a string (varchar), and must be in single quotes in your SQL

  • Thanks! The error was this small detail of the simple quotes! error appeared when place echo mysqli_error($con);

  • Keep in mind that the value of $tel=9999-99999; will return -90000. To return 9999-99999 put it in quotes according to the other $tel="9999-99999";

Show 1 more comment

2 answers

2


There is probably an error with your SQL that is not natively reported by PHP. To see the error generated in Mysql use the php function mysqli_error http://php.net/manual/en/mysqli.error.php

Put a echo mysqli_error($con); after you run the query (and before closing the connection).

  • 1

    Thanks! It was an error in SQL syntax that appeared thanks to your tip :)

0

It can be incompatibility of types in your table. Ex: trying to insert a varchar in an int, make sure that’s not it. If not, use the function mysqli_error.

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

Browser other questions tagged

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