Mysql 8.0 Database Registry with PHP 8.0.5

Asked

Viewed 58 times

-1

Fala galera!

I am a beginner in PHP and am unable to insert data into my Mysql database.

Just follow my code:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="content-type">
        <title>Exemplo PHP</title>
    </head>
    <body>
        <?php
        echo "<h1>Adição de usuário</h1>";
        $conexao = mysqli_connect('localhost', 'root', ' ');
        if(!$conexao) {
            echo "<p><b>Não foi possível conectar-se ao banco de dados</b></p>";
            echo mysqli_error();
        }
        else {
            mysqli_select_db('db_exemplo', $conexao);
            $sql = mysqli_query("INSERT INTO usuarios(Usuario, Senha) VALUES ('ADMIN','SENHA')");
            if(!$sql)
                echo "<h2>Não foi possível cadastrar o usuário!</h2>";
            else 
                echo "<h2>Usuário cadastrado com sucesso!</h2>";
        }
        mysqli_close($conexao);
        ?>
    </body>
</html>

When I open the page in the browser, it returns me only "User Addition" and also I already performed the query in the database and no record happened.

Does anyone have any idea what might be going on?

Thank you.

1 answer

0


Pedro, is missing vc pass the connector in function mysqli_query($conn, $sql)

Leave it this way it will work:

$sql = mysqli_query($conexao, "INSERT INTO usuarios(Usuario, Senha) VALUES ('ADMIN','SENHA')");
  • 1

    Rafael, thank you so much! You gave me a light. The way you guided me, still continued not returning me anything, what I did was the following, where is made the connection with the bank I changed to $conexao = mysqli_connect('localhost', 'root', ' ', 'db_exemplo'); then eliminated the line mysqli_select_db('db_exemplo', $conexao); and passed the connector to the function mysqli_query($conn, $sql) and it worked.

  • Perfect, @Pedro. One of the parameters mysqli_connect() is the database, in this case, your db_exemplo. When in doubt, go to the manual and the examples. It may not solve but shed some light on the problem. https://www.php.net/manual/en/mysqli.construct.php

Browser other questions tagged

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