Does not write records to my database

2

I created a registration system, but when I click the register button the message "Could not insert record:" ... Someone can identify this error, because I have not been able to identify it yet. Follow my code below.

cadastro.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>

cadastrando.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>
    
asked by anonymous 13.05.2015 / 12:55

4 answers

2

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 need to set the ID / Name of submit button in HTML.

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

What this does:

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

is to search for a field called submit in the fields of the form that was submitted. The form you have in your question does not have any field with that name. If you do print_r($_POST) you can query the fields in the $_POST variable.

Now I noticed that this code has more wrong things.

else is missing here, before the die line:

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());
}
    
13.05.2015 / 13:11
3

Hello, honestly this 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 was defined to type POST with name submit when you did not even specify a name attribute.

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

The correct thing would be to do something like this on 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)
    
13.05.2015 / 14:24
3

It seems like you did not pass the correct values in the insert but only strings , add the dollar sign in the variables

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

For values of form to be recognized by php it is mandatory to enter the name attribute, its submit does not have it.

Add a name , in this case you should call submit as in if

<input type="submit" name="submit" value="cadastrar">
    
13.05.2015 / 13:46
2

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

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

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

    
13.05.2015 / 14:18