Does not record records in my database

Asked

Viewed 6,496 times

5

I created a registration system, but when I click the sign up button appears the message "Could not insert record:"... Someone can identify this error as I have so far been unable to identify it. Follow my code below.

php.

<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="utf-8">
<title>Sistema de cadastro</title>
</head>
<body>
<form name="signup" method="post" action="cadastrando.php">
nome: <input type="text" name"nome" /><br /><br />
sobrenome: <input type="text" name="sobrenome" /><br /><br />
e-mail: <input type="text" name="email" /><br /><br />
senha: <input type="password" name="senha" /><br /><br />
<input type="submit" value="cadastrar">
</form>
</body>
</html>

registering.php

  <!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="utf-8">
<title>Cadastrando...</title>
</head>
<body>

<?php


$servidor = 'localhost';
$usuario = 'root';
$senha = null;
$banco = 'dbcadastro';
$conexao = mysqli_connect($servidor,$usuario,$senha,$banco);
// verifica a conexao
if (mysqli_connect_errno())
  {
  echo "Erro ao conectar: " . mysqli_connect_error();
  }
?>
  <?php

if (isset($_POST["submit"])) {
    $nome = $_POST['nome'];
    $sobrenome = $_POST['sobrenome'];
    $email = $_POST['email'];
    $senha = $_POST['senha'];
    $sql = mysqli_query($conexao,"INSERT INTO `usuarios`('', 'nome', 'sobrenome', 'email', 'senha'");
}
{
  die("<br />Nao foi possivel inserir registro: " . mysql_error());
}
echo "<br />Um novo registro foi feito!";

?>




</body>
</html>
  • 1

    the question code has already been edited, so the error that generated the question doesn’t even make sense anymore. The question you asked is already answered, so you’ve already changed the HTML part. you have many other errors in the code, which have already been mentioned by several people, but if you have other mistakes, create new questions.

  • Ruan, I’m glad you found the solution to your [en.so] problem! However, just a hint, do not need to tidy the code of the question, otherwise whoever sees the question later will not understand what happened. Hug!

  • Okay, I won’t do the edits,!!

5 answers

5

Looks like you didn’t pass the correct values on insert and yes only strings, add the dollar sign to the variables

sql = mysqli_query($conexao,"INSERT INTO `usuarios` VALUES('', '$nome', '$sobrenome', '$email', '$senha'") or die(mysqli_error($conexao));

For the values of form be recognized by php is required to enter the attribute name, your submit he doesn’t have it.

Add a name, in case it should be called submit like this in the if

<input type="submit" name="submit" value="cadastrar">
  • Continued to appear error message =/ ^^

  • There was VALUES there? insert into usuarios VALUES ()...

  • @Ruanrodrigues and the error is the same? if it is, you did not correct the <input> correctly. if it is another, it is because the query is wrong, and it is. but this no longer concerns the error of the question you asked.

  • <input type="Submit" name="Submit" value="register">, that’s right?

  • @Ruanrodrigues as I told you in my reply, make print_r to the post and confirm.

  • Array ( [last name] => Rodrigues [email] => [email protected] [password] => 123456 [Submit] => register ) I think the error is in my $name variable, because it is not showing up...

  • if this is what appears in print_r, then the error that was appearing when you asked the question no longer appears.

  • @Ruanrodrigues in my answer I put another error that is in the code. and the query is wrong, but this no longer concerns the question asked.

  • @rafael withoeft, thanks for the correction, I edited the reply.

  • @rray For nothing :)

  • Good... I tried all the more solutions to still appear the "It was not possible to insert record:"

  • The die() is inside the Else or loose?

  • he’s on the loose.

  • You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near '' at line 1 @rray , this appeared after it was edited

  • Replace'' with null in VALUES(''.

Show 10 more comments

4


If you do print_r($_POST) you will see that there is no variable called 'submit' in the array of $_POST. That’s the problem. You have to set the ID/ Name of the submit button in HTML.

<input type="submit" value="cadastrar"/> 

What does this do:

if (isset($_POST["submit"]))

is to search for a field called Submit in the fields of the form that was submitted. The form that you have in your question has no field with that name. If you do print_r($_POST) can consult the existing fields in the variable $_POST.

Now I’ve noticed that there are more things wrong with that code.

Missing one else here, before the line of die:

if (isset($_POST["submit"])) {
    $nome = $_POST['nome'];
    $sobrenome = $_POST['sobrenome'];
    $email = $_POST['email'];
    $senha = $_POST['senha'];
    $sql = mysqli_query($conexao,"INSERT INTO `usuarios`('', 'nome', 'sobrenome', 'email', 'senha'");
}
{
  die("<br />Nao foi possivel inserir registro: " . mysql_error());
}

4

Hello, honestly that was something "OMG". Your error is in the HTML form, in input de envio, you wrote something like this:

<input type="submit" value="cadastrar"/> 

And then you tried to check with PHP if any variable has been defined to type POST by name submit when you haven’t even specified an attribute name.

if (isset($_POST["submit"])){
...

The right thing would be to do something like this in HTML:

<input type="submit" name="submit" value="cadastrar"/> 

And also missing the VALUES of SQL, see the correct syntax:

INSERT INTO tabela (campo1, campo2) VALUES (valor1, valor2)

3

This SQL query is wrong, you did not specify to which columns you are inserting which value is very likely to be inserted value of one column in another and giving a type error, it is also not with the VALUES the appropriate form would be:

"INSERT INTO usuarios (coluna1,coluna2,coluna3,coluna4) VALUES ('$nome', '$sobrenome', '$email', '$senha')";

Replace the colunas by the columns where they will store the values, remember that they must be in the same order coluna / valor

-2

Simplify your code so it can find a semantics, use coherent conditions so that your algorithm is easy to interpret for you and others to re-use / modify / improves.

if (!empty=$nome && !empty=sobrenome...................) {

$sql = "INSERT INTO table (nome,email..........) VALUES ($nome,$email.......)";

}else
{
  die(trigger_error("<br />Nao foi possivel inserir registro: " . mysql_error()));
}
  • 1

    !empty=$nome is not a valid syntax in PHP

Browser other questions tagged

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