Error inserting into Mysql database

Asked

Viewed 217 times

0

I started to study a little about web development, but I have a problem trying to insert into the table. There is an error, when I click on a dps register, it displays this message in the browser :

 query("INSERT INTO nafcliente (nome,local) VALUES ('$nome' , '$local') ") or die ($mysqli->error); } 

That would be my index,

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.php page

<?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);
}

  • When inserting more information, edit the question and include it directly

  • You can leave, thanks for the warning

2 answers

0

<?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 it like this, maybe I’m wrong for my part, it’s been a long time since I’ve played with php, but I’ve seen a code of mine that’s like this, it doesn’t hurt to try ;)

  • Opa, I tested here...the browser error is gone, but no record appears in the database Mysql did not return any record. (The consultation took 0.0000 seconds.)

0

I see that only in Query $mysqli-> query("SQL") or die ($mysqli->error); that is missing link reference.

$mysqli-> query($mysqli, "SQL") or die ($mysqli->error);

And also in 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/en/function.mysqli-connect.php

  • You’re using PDO, right? For a simple system you can simplify and use only mysqli which is also a safe class and very similar to the syntax that has been discontinued.

  • Beauty I will test these tips, thank you

  • Mysqli_connect is very nice and simple. use in all my applications.

  • The browser error is at least gone, but no record is found in the table

  • Right! I will edit the answer with a proposal only with Mysqli.

  • Thank you very much, old man

  • The browser displays: error); }} , which could be?

  • Ah pq has an extra } . Just delete one.

  • Oops, thank you very much for the touches

Show 4 more comments

Browser other questions tagged

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