Error while saving records in the database

Asked

Viewed 401 times

0

I have a normal form with a normal page to import the information to a normal bank table, but for some reason it falls straight into the die and does not save the information.

Form:

<form method="POST" action="update-cli.php">
<div class="container">
  <div class="row">
    <div class="col-lg-8 col-md-10 mx-auto">
      <div class="form-group">
        <label for="NOME">Nome:</label>
        <input type="text" class="form-control" id="NOME" name="NOME" placeholder="Nome do cliente">
      </div>
      <div class="form-group">
        <label for="DATA">Data de nascimento:</label>
        <input type="text" class="form-control" id="DATA" name="DATA" placeholder="Data de Nascimento do cliente">
      </div>
        <div class="form-group">
        <label for="ENDERECO">Endereço:</label>
        <input type="text" class="form-control" id="ENDERECO" name="ENDERECO" placeholder="Endereço do consultório">
      </div>
      <div class="form-group">
        <label for="TELEFONE">Telefone:</label>
        <input type="text" class="form-control" id="TELEFONE" name="TELEFONE" placeholder="Telefone fixo">
      </div>
      <div class="form-group">
        <label for="CELULAR">Celular:</label>
        <input type="text" class="form-control" id="CELULAR" name="CELULAR" placeholder="Telefone celular">
      </div>
      <button type="submit" class="btn btn-primary btn-lg btn-block">Salvar</button>
    </div>
  </div>
</div>
</form>

Import page:

<?php

    $nome = $_POST['NOME'];
    $data = $_POST['DATA'];
    $endereco = $_POST['ENDERECO'];
    $telefone = $_POST['TELEFONE'];
    $celular = $_POST['CELULAR'];

    $strcon = mysqli_connect('localhost','root','', 'sis_tam') or die('Erro ao conectar ao banco de dados');
    $sql = "INSERT INTO clientes VALUES ('$nome', '$data', '$endereco', '$telefone', '$celular')"; 
    mysqli_query($strcon,$sql) or die("Erro ao tentar cadastrar registro");
    mysqli_close($strcon);

    echo '<script type="text/javascript">
            alert("Salvo com Sucesso !");
            window.history.go(-1);
        </script>';

?>

Table in the bank:

ID NAME DATE ADDRESS CELL PHONE

Can anyone point out the problem? Thank you

Bank structure: inserir a descrição da imagem aqui

  • What error is being shown?

  • Database connection data is correct?

  • How is the sql structure of the table in the database?

  • The error that gives is "Error trying to register", the one I put in "or die".

1 answer

2


Your code SQLis generating error 1136: Column Count doesn’t match value Count at Row 1

This is because your table has 6 columns and you are passing 5, by more than the ID be auto-increment, you need to specify in which columns you will write the data, your $sql should look like this

$sql = "INSERT INTO clientes (NOME, DATA, ENDERECO, TELEFONE, CELULAR) VALUES ('$nome', '$data', '$endereco', '$telefone', '$celular')";

To find out this kind of mistake the easy way is to give a echo in its variable $sql and run the generated code directly in the database manager, it will probably point the error to you.

I hope it helps!

  • I had imagined that the problem was not having passed the id, but I didn’t know how to pass if I didn’t have a field with the name ID. Thanks, it helped a lot! :)

Browser other questions tagged

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