Register multiple records using the same form

0

I have a customer registration form where it is possible to register one client at a time, how do I register two clients for example at one time using this same form? I tried to put one more input, but only the last record.

form.php

<html>
<head>
<title> Cadastro de Usuário </title>
</head>
<body>
<form method="POST" action="cadastro.php">
<label>Nome:</label><input type="text" name="nome" id="nome"><br>
<label>Sobrenome:</label><input type="text" name="sobrenome" id="sobrenome"><br>
<input type="submit" value="Cadastrar" id="cadastrar" name="cadastrar">
</form>
</body>
</html>

<?php$nome=$_POST['nome'];$sobrenome=$_POST['sobrenome'];$connect=mysql_connect('localhost','root','');$db=mysql_select_db('painel2');$query_select="SELECT nome FROM clientes WHERE nome = '$nome'";
$select = mysql_query($query_select,$connect);
$array = mysql_fetch_array($select);
$logarray = $array['nome'];

  if($nome == "" || $nome == null){
    echo"<script language='javascript' type='text/javascript'>alert('O campo nome deve ser preenchido');window.location.href='formulario.php';</script>";

    }else{
      if($logarray == $nome){

        echo"<script language='javascript' type='text/javascript'>alert('Esse nome já existe');window.location.href='formulario.php';</script>";
        die();

      }else{
        $query = "INSERT INTO clientes (nome,sobrenome) VALUES ('$nome','$sobrenome')";
        $insert = mysql_query($query,$connect);

        if($insert){
          echo"<script language='javascript' type='text/javascript'>alert('Cliente cadastrado com sucesso!');window.location.href='formulario.php'</script>";
        }else{
          echo"<script language='javascript' type='text/javascript'>alert('Não foi possível cadastrar esse cliente');window.location.href='formulario.php'</script>";
        }
      }
    }
?>
    
asked by anonymous 18.01.2018 / 17:47

1 answer

1

One solution would be to put the inputs to send the values as an array.

For this you can include and modify these lines in the html file

<form method="POST" action="cadastro.php">
<label>Nome:</label><input type="text" name="nome[]" id="nome1"><br>
<label>Sobrenome:</label><input type="text" name="sobrenome[]" id="sobrenome1">
<br>
<label>Nome:</label><input type="text" name="nome[]" id="nome2"><br>
<label>Sobrenome:</label><input type="text" name="sobrenome[]" id="sobrenome2">
<br>
<label>Nome:</label><input type="text" name="nome[]" id="nome3"><br>
<label>Sobrenome:</label><input type="text" name="sobrenome[]" id="sobrenome3">
<br>
<input type="submit" value="Cadastrar" id="cadastrar" name="cadastrar">
</form>

You can improve the look by hiding the fields for example with css and having a button to make them appear, as it is not the focus of the question, I will not include that.

And in the PHP file you can use a foreach or a for to go through the arrays, in this case I will use the for as an example.

$nome = $_POST['nome'];
$sobrenome = $_POST['sobrenome'];

$connect = mysql_connect('localhost','root','');
$db = mysql_select_db('painel2');

for($i=0;$i < count($nome);$i++){
    $query_select = "SELECT nome FROM clientes WHERE nome = '$nome[$i]'";
    $select = mysql_query($query_select,$connect);
    $array = mysql_fetch_array($select);
    $logarray = $array['nome']; 

    if($nome[$i] == "" || $nome[$i] == null){
    echo"<script language='javascript' type='text/javascript'>alert('O campo nome deve ser preenchido');window.location.href='formulario.php';</script>";

    }else{
      if($logarray == $nome[$i]){

        echo"<script language='javascript' type='text/javascript'>alert('Esse nome já existe');</script>";
          if($i == (count($nome)-1){
              die();
          }else{
              continue();
          }

      }else{
        $query = "INSERT INTO clientes (nome,sobrenome) VALUES ('$nome[$i]','$sobrenome[$i]')";
        $insert = mysql_query($query,$connect);

        if($insert){
          echo"<script language='javascript' type='text/javascript'>alert('Cliente cadastrado com sucesso!');'</script>";
        }else{
          echo"<script language='javascript' type='text/javascript'>alert('Não foi possível cadastrar esse cliente');'</script>";
        }
      }
    }
}

I did not test, but should work.

I hope it helps.

    
19.01.2018 / 12:01