How to Save multiple records with PHP

By | April 4, 2015

Save multiple records with PHP

Okay guys , office happened to me that I have asked you to create,save multiple records from one form to expedite the rise of information, as doing so one by one would take longer . EG: our registered user requires all employees of the company ..

So in this example we will create 3 students from the same form using PHP our friend . Let our form layout using Bootstrap and will make a loop to make our form repeat 3 times which will be within the etiquteas <form > .

<form action="?c=Alumno&a=CrearMultiple" method="post" enctype="multipart/form-data">
    
    <div class="row">
        <?php $x = 1; for($x; $x <= 3; $x++): ?>
        <div class="col-xs-4">
            <div class="well well-sm">
                <div class="form-group">
                    <label>Nombre</label>
                    <input type="text" name="Nombre[]" class="form-control" placeholder="Ingrese su nombre" data-validacion-tipo="requerido|min:3" />
                </div>

                <div class="form-group">
                    <label>Apellido</label>
                    <input type="text" name="Apellido[]" class="form-control" placeholder="Ingrese su apellido" data-validacion-tipo="requerido|min:10" />
                </div>

                <div class="form-group">
                    <label>Correo</label>
                    <input type="text" name="Correo[]" class="form-control" placeholder="Ingrese su correo electrónico" data-validacion-tipo="requerido|email" />
                </div>

                <div class="form-group">
                    <label>Sexo</label>
                    <select name="Sexo[]" class="form-control">
                        <option value="1">Masculino</option>
                        <option value="2">Femenino</option>
                    </select>
                </div>

                <div class="form-group">
                    <label>Fecha de nacimiento</label>
                    <input readonly type="text" name="FechaNacimiento[]" class="form-control datepicker" placeholder="Ingrese su fecha de nacimiento" data-validacion-tipo="requerido" />
                </div>

                <div class="row">
                    <div class="col-xs-6">
                        <div class="form-group">
                            <label>Foto</label>
                            <input type="file" name="Foto[]" placeholder="Ingrese una imagen" />
                        </div>     
                    </div>
                </div>  
            </div>
        </div>
        <?php endfor; ?>
    </div>
    
    <hr />
    
    <div class="text-right">
        <button class="btn btn-success btn-lg btn-block">Guardar</button>
    </div>
</form>

Now each name of the html controls : input , select, file , checkbox, etc .. must say that you are an Array , so we add the names brackets

<select name="Sexo[]" class="form-control">

 This way when you press the submit button , send all information contained in an array , and our controller will perform the logic for keeping records

 The parameters passed are passed as follows :

array(size=5)
  'Nombre' => 
    array(size=3)
      0 => string 'Eduardo' (length=7)
      1 => string 'Alberto' (length=7)
      2 => string 'Alberto' (length=7)
  'Apellido' => 
    array(size=3)
      0 => string 'Rodriguez Díaz' (length=15)
      1 => string 'Rodriguez Patiño' (length=17)
      2 => string 'Ibanez Fender' (length=13)
  'Correo' => 
    array(size=3)
      0 => string 'asd@asd.net' (length=11)
      1 => string 'abasd@asd.net' (length=13)
      2 => string 'asd@asd.net' (length=11)
  'Sexo' => 
    array(size=3)
      0 => string '1' (length=1)
      1 => string '1' (length=1)
      2 => string '1' (length=1)
  'FechaNacimiento' => 
    array(size=3)
      0 => string '2015-04-01' (length=10)
      1 => string '2015-04-02' (length=10)
      2 => string '2015-04-16' (length=10)

 Our Student driver does the following , exactly your Create Multiple action:

 Our Student driver does the following , exactly your Create Multiple action:

What we are doing is telling it for 3 laps, in our case we do a COUNT to calculate how many laps should give.

Then we instantiate our Student class and begin to fill values.

To understand, the index [‘name’] contains an array of three values and referring to the 3 names entered in our 3 forms, then at every turn the variable $ i FOR our store that value in the Name property Pupil class.

Then do the INSERT calling our model student

And last of all we do a redirect to the main view of the web.

To better understand this example can give it a read to this entry Crud advancing MVC for PHP, as this is based on the MVC architecture and explains that link.

The example was carried out using: jquery | jquery UI | bootstrap | MVC

Update: you can see the following POST Insert multiple records from the same form with PHP and jQuery where we do the same but adding functionality using jQuery to add N records from the same form.