Error inserting into Mysql database
I started studying a bit about web development, but I have a problem trying to insert into the table. It presents an error, when I click on a dps register, it presents this message in the browser:
query("INSERT INTO nafcliente (nome,local) VALUES ('$nome' , '$local') ") or die ($mysqli->error); }
That would be my index, I did pretty simple just to test
Html
<html>
<head></head>
<body>
<form action="process.php" method="POST">
<input type="text" name="nome" placeholder="Digite seu nome" />
<input type="text" name="local" placeholder="Digite seu endereço" />
<input type="submit" name="Cadastrar" value="Cadastrar" />
</form>
</body>
</html>
This is my process Page.php
<?php
$mysqli= new mysqli("localhost","root","","cad") or die(mysqli_error($mysqli));
if(isset($_POST['Cadastrar'])){
$nome= $_POST['nome'];
$local= $_POST['local'];
$mysqli-> query("INSERT INTO cliente (nome,local) VALUES ('$nome' , '$local') ") or
die ($mysqli->error);
}
2 answers
<?php
$mysqli= new mysqli("localhost","root","","cad") or die(mysqli_error($mysqli));
if(isset($_POST['Cadastrar'])){
$nome= $_POST['nome'];
$local= $_POST['local'];
$query = mysqli_query($mysqli,"INSERT INTO cliente (nome,local) VALUES ('$nome' , '$local') ");
if(!query){
echo("error");
}
Try like this, maybe it's wrong my part, it's been a long time that I do not mech with php, but I saw a code of mine that is like this , it does not cost to try ;)
I see that only in Query $mysqli-> query("SQL") or die ($mysqli->error);
that is missing referencing the connection.
$mysqli-> query($mysqli, "SQL") or die ($mysqli->error);
And also in the connection should be mysqli_connect
and not just mysqli
$mysqli= new mysqli_connect ("localhost","root","","cad") or die(mysqli_error($mysqli));
Proposal in Mysqli:
<?php
$mysqli= mysqli_connect ("localhost","root","","cad") or die(mysqli_error($mysqli));
if(isset($_POST['Cadastrar'])){
$nome= $_POST['nome'];
$local= $_POST['local'];
mysqli_query($mysqli, "INSERT INTO cliente (nome,local) VALUES ('$nome' , '$local') ") or
die ($mysqli->error);
}}
Documentation: https://www.php.net/manual/pt_BR/function.mysqli-connect.php