Does not insert information into the database

Asked

Viewed 62 times

1

Good afternoon, I am trying to enter data in a database and I have as return the error message: "It was not possible to register the user".

I’m using Xamp software, with Apache and Mysql installed.

The code is as follows:

<head>

    <meta charset="UTF-8">
    <meta http-equiv="content-type">
    <title>Adicao Usuario Banco de Dados</title>

</head>

<body>

    <?php

    echo "<h1>ADICAO DE USUARIO</h1>";
    $conexao_bd = mysql_connect("localhost");

    if(!$conexao_bd) {

        echo "<p><b>Não foi possível conectar ao Banco de Dados</b></p>";
        echo mysql_error();
    }

    else {
          mysql_select_db("db_hotel",$conexao_bd);
          $retorno = mysql_query("INSERT INTO usuarios(Nome_Usuario,Senha_Acesso) VALUES('ADMIN','M45T3R')");
        if(!$retorno)
          echo "<h2>Nao foi possivel cadastrar o usuario!</h2>";
        else
          echo "<h2>Usuario cadastrado com sucesso!</h2>";
        }
    mysql_close($conexao_bd);

    ?>

</body>

If you can help me, from now on....

  • Show what’s inside '$return'. Give a var_dump($return). Maybe it’s a connection error with your bd.

  • 3

    Hi @Walter. Catch the bug with the code echo(mysqli_error($conexao_bd)); in the if(!$retorno) and put in your question.

  • Change echo "<h2>Nao foi possivel cadastrar o usuario!</h2>"; for echo mysql_error();

  • 1

    Which version of PHP you are using?

1 answer

0

The mysql_connect Open a connection to a Mysql server. Try adding the user and password parameters. Example:

$conexao_bd = mysql_connect('localhost', 'usuario', 'senha');

Since you are using xampp, you can check which user is attached to the localhost, then we will check where to find the user and password.

inserir a descrição da imagem aqui

Note that in this example the user of localhost is the root and we don’t have a password. That is, the connection opening code with the bank would be:

$conexao_bd = mysql_connect("localhost","root",""); //host, usuario, senha vazia

Browser other questions tagged

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