Data insertion problem in mysql table

Asked

Viewed 601 times

1

I’m having a problem to insert data into a table someone can show me the error.

<html>

<head></head>

<body>
  <div>
    <form method="POST" action="varcadastro.php">
      <p class="contact">
        <label for="name">Nome</label>
      </p>
      <input id="name" name="nome" placeholder="Primeiro e Ultimo nome" type="text">

      <p class="contact">
        <label for="datanasc">Data Nascimento</label>
      </p>
      <input id="email" name="datanasc" placeholder="22041996" type="date">
      <p class="contact">
        <label for="username">Usuario</label>
      </p>
      <input id="username" name="usuario" placeholder="usuario" type="text">
      <p class="contact">
        <label for="password">Senha</label>
      </p>
      <input type="password" id="senha" name="senha">
      <p class="contact">
        <label for="cpf">cpf</label>
      </p>
      <input id="cpf" name="cpf" type="cpf">
      <p class="contact">
        <label for="cidade">cidade</label>
      </p>
      <input id="cidade" name="cidade" type="cidade" <p class="contact">
      <label for="bairro">bairro</label>
      </p>
      <input id="bairro" name="bairro" type="bairro">

      <p class="contact">
        <label for="rua">rua</label>
      </p>
      <input id="rua" name="rua" type="rua">
      <p class="contact">
        <label for="numero">numero</label>
      </p>
      <input id="numero" name="numero" type="numero">
      <p class="contact">
        <label>Telefone Residencial</label>
      </p>
      <input name="telefone1" placeholder="phone number" type="text">
      <br>
      <p class="contact">
        <label>Telefone Celular</label>
      </p>
      <input id="phone" name="telefone2" placeholder="phone number" type="text">
      <br>
      <p class="contact">
        <label for="phone">Telefone Opcional</label>
      </p>
      <input id="phone" name="telefone3" placeholder="phone number" type="text">
      <br>
      <input name="submit" value="Cadastrar" type="submit">
    </form>
</body>

</html>

PHP:

<?php
    $nome= $_POST["nome"];
    $datanasc= $_POST["datanasc"];
    $rua= $_POST["rua"];
    $usuario= $_POST["usuario"];
    $senha= $_POST["senha"];
    $cpf= $_POST["cpf"];
    $numero= $_POST["numero"];
    $cidade= $_POST["cidade"];
    $telefone1= $_POST["telefone1"];
    $telefone2= $_POST["telefone2"];
    $telefone3= $_POST["telefone3"];
    $bairro= $_POST["bairro"];
    $conexao = @mysql_connect("localhost","root"); //localhost é onde esta o banco de dados.
if (!$conexao)
die ("Erro de conexão com localhost, o seguinte erro ocorreu -> ".mysql_error());
 //conectando com a tabela do banco de dados
$banco = mysql_select_db("biblioteca",$conexao); //nome da tabela que deseja que seja inserida os dados cadastrais
if (!$banco)
die ("Erro de conexão com banco de dados, o seguinte erro ocorreu -> ".mysql_error());
$query = "INSERT INTO `manterusuario` (`nome`, `telefone1`, `telefone2`, `telefone3`, `cpf`, `datanasc`, `usuario`, `senha` , `cidade` , `rua` , `bairro` , numero` ) VALUES (`$nome`, `$telefone1`, `$telefone2`, `$telefone3`, `$cpf`, `$datanasc`, `$usuario`, `$senha` , `$cidade` , `$rua` , `$bairro` , $numero` )";
mysql_query($query,$conexao);
echo "Cadatro realizado com sucesso!Obrigado!";
?>

My table is like this:

CREATE TABLE manterusuario (
  codigousuario INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  manterbiblioteca_codigobiblioteca INTEGER UNSIGNED NOT NULL,
  nome VARCHAR(50) NOT NULL,
  telefone1 VARCHAR(50) NOT NULL,
  telefone2 VARCHAR(50) NOT NULL,
  telefone3 VARCHAR(50) NULL,
  cpf VARCHAR(50) NOT NULL,
  datanasc DATE NOT NULL,
  login VARCHAR(20) NOT NULL,
  senha VARCHAR(20) NOT NULL,
  cidade VARCHAR(50) NULL,
  rua VARCHAR(50) NULL,
  bairro VARCHAR(50) NULL,
  numero VARCHAR(10) NULL,
  PRIMARY KEY(codigousuario),
  INDEX manter_usuario_FKIndex1(manterbiblioteca_codigobiblioteca)
);
  • All fields in your table are varchar?

  • Alias, post the modeling of your table manterusuario ai.

  • Marcelo, click [Edit] and add to the question.

  • Good evening Marcelo, I don’t understand, used the mysqli tag, but your code has nothing with php’s MYSQLI API, this is confusing. Tell which error occurs?

  • In INSERT SQL near the end, this , $numero\`` com o `` at the end but not at the beginning is very suspicious, you do not think?

1 answer

2


Do not use the functions mysql_* are obsolete and have been removed from php7, do not use @ it hides errors which makes it difficult to detect the problem.

If you’re a legacy project all right, always add or die(mysql_error()) along with the mysql_query() so it is possible to know if the error is in the database or in php code.

Your Internet problem is backtick(``) are used only in table or column names and not in values. After values exchange backticks for single quotes(')

Wrong:

INSERT INTO `manterusuario` (`nome`, `telefone1`, `telefone2`, `telefone3`, `cpf`, `datanasc`, `usuario`, `senha` , `cidade` , `rua` , `bairro` , `numero` )
 VALUES (`$nome`, `$telefone1`, `$telefone2`, `$telefone3`, `$cpf`, 
`$datanasc`, `$usuario`, `$senha` , `$cidade` , `$rua` , `$bairro` , $numero` )

Right:

INSERT INTO `manterusuario` (`nome`, `telefone1`, `telefone2`, `telefone3`, `cpf`, `datanasc`, `usuario`, `senha` , `cidade` , `rua` , `bairro` , `numero` ) 
VALUES ('$nome', '$telefone1', '$telefone2', '$telefone3', '$cpf', '$datanasc', '$usuario', '$senha' , '$cidade' , '$rua' , '$bairro' , '$numero' )

If you need to check the date of d/m/Y for Y-m-d see that answer

Recommended reading:

Why should we not use mysql type functions_*?

Why do they say using @arroba to suppress errors is a bad practice?

Mysqli vs PDO - which is the most recommended to use?

  • 1

    Not to mention that he is adding the date the way it is coming in BR format, and not in the correct database format.

  • Vlw I already got here

  • 1

    @Marcelogomes If the answer solved the problem mark it as correct, thank you!

Browser other questions tagged

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