Save more than one data in this form with PHP and Mysqli

Asked

Viewed 183 times

0

Hi, I have a problem with this code. I’m making a form I need to save, but unfortunately in the database only saved once the rest always gives error... I would like to resolve this problem, and I thank you for reading it. Below contains the first part of the code

<?php
   session_start();
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Formulário</title>
</head>
  <body>
    <?php
        if(isset($_SESSION['msg'])){
            echo $_SESSION['msg'];
            unset ($_SESSION['msg']);
        }
    ?>
    <form method="post" action="registroCPP.php">
      <input type="text" name="Nome" id="Nome" placeholder="Nome"/><br>
      <input type="text" name="Sobrenome" id="Sobrenome" placeholder="Sobrenome"/><br>
      <input type="text" name="Email" id="Email" placeholder="E-mail"/><br>
      <input type="text" name="Idade" id="Idade" placeholder="Idade"/></br>
      <input type="text" name="Celular" id="Celular" placeholder="Número de Celular"/><br>
      <input type="text" name="Endereco" id="Endereco" placeholder="Endereço"/><br>
      <input type="text" name="numero_endereco" id="numero_endereco" placeholder="Número"/><br>
      <input type="text" name="complemento" id="complemento" placeholder="Complemento"/><br>
      <input type="text" name="cargo" id="cargo" placeholder="Cargo/ Emprego"/><br>
      <input type="submit" value="Envie">
    </form>
  </body>
</html>

The party receiving the data from this Ubmit:

<?php
session_start();
include 'conecta_mysql.ini';

$nome = $_POST['Nome']; 
$sobrenome = $_POST['Sobrenome'];
$email = $_POST['Email'];
$idade = $_POST['Idade'];
$celular =  $_POST['Celular'];
$endereco =  $_POST['Endereco'];
$numend = $_POST['numero_endereco'];
$complemento = $_POST['complemento'];
$cargo =  $_POST['cargo'];

$sql = "INSERT INTO usuario (Nome, Sobrenome, Email, Idade, Celular, Endereço, NumeroEndereco, ComplementoEndereco, Cargo) VALUES ('$nome','$sobrenome','$email','$idade','$celular', '$endereco', '$numend', '$complemento', '$cargo')";
if($conexao->query($sql) === TRUE) {
    $_SESSION ['msg'] = "Usuário cadastrado";
    header('Location: formulario.php');
}else{
    echo "erro";
}
$conexao->close();

?>

The last part of the code that is the connection to the database.

<?php

$conexao = mysqli_connect('localhost', 'root','','iep');

mysqli_set_charset($conexao, 'utf8');

if($conexao -> connect_error){
    die("Falha ao realizar a conexão: ".$conexao -> connect_error);
}?>
  • What mistake it makes?

  • 1

    Perhaps in your database the primary key of the 'user' table is not set to AUTO_INCREMENT. You have already checked this?

  • I checked the database, and really had not set. Now it went all right, grateful Laubf

1 answer

2

Friend as you are using only php both separately and it within html by Session your code gets "poor", so you can not validate the data correctly.

Recommend that you use ajax to call php, you will have more freedom to handle php return.

Browser other questions tagged

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