-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.
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 linemysqli_select_db('db_exemplo', $conexao);
and passed the connector to the functionmysqli_query($conn, $sql)
and it worked.– Pedro
Perfect, @Pedro. One of the parameters
mysqli_connect()
is the database, in this case, yourdb_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– Rafael Vidal