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

0

Hello, I have a problem with this code. I am making a form that I need to save, but unfortunately in the database it only saves once the rest always gives error ... I would like to solve this problem, since I thank you for having read it. Below is 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 that receives the data from this submit:

<?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);
}?>
    
asked by anonymous 13.06.2018 / 06:21

1 answer

2

Friend as you are using only php both he separated as he inside the html by session your code gets "poor", this way you can not validate the data correctly.

I recommend using ajax to call php, you will have more freedom to handle the return of php.

    
13.06.2018 / 22:42