Register multiple records using the same form

Asked

Viewed 769 times

0

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

php form.

<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>

inserir a descrição da imagem aqui

<?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>";
        }
      }
    }
?>
  • I don’t know if I understand very well, you want to have more fields where other names can be inserted?

  • That’s right, if I could insert 10 names on that screen for example it would be perfect

1 answer

1


A solution would be to place inputs to send 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, hiding the fields for example with css and have a button to make them appear, as it is not the focus of the question, I will not include this.

And in the PHP file you can use a foreach or a for to browse the arrays, in this case I will use for 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 didn’t get to test it, but it should work.

I hope it helps.

  • Thank you worked well!

Browser other questions tagged

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