send array to database via php

Asked

Viewed 267 times

-1

Well, I have a form with a button to repeat it several times, so I had to create arrays in it. Only now I don’t know how to send it to the database. Searching I found that the way it is today is as if I tried to send the array itself to the database and not the information in it. I’m new so I’m pretty lost in it.

 <?php
require_once 'db_connect.php';
if(isset($_POST['btn-cadastrar'])):
    $nomeFicha = $_POST['nomeFicha'];
    $nomeExercicio = $_POST['exercicio'];
    $grupoMuscular = $_POST['grupoMusc'];
    $numSeries =  $_POST['numSeries'];
    $numRepeticoes =  $_POST['numRep'];
    $peso=$_POST['peso'];
    $sql = "INSERT INTO ficha (nomeFicha, nomeExercicio, grupoMuscular, numSeries, numRepeticoes, peso) VALUES('$nomeFicha', '$nomeExercicio', '$grupoMuscular', '$numSeries', '$numRepeticoes', 'peso')";

html form:

<form class="needs-validation" novalidate action="php_action/create.php" method="POST">        
<div id="divContent" class="container-fluid">
  <div class="form-row">
    <div class="col-md-5 mb-3">
      <label for="validationCustom01">Nome da ficha</label>
      <input type="text" class="form-control" id="validationCustom01" name="nomeFicha" placeholder="Nome da ficha" required>
      <div class="valid-feedback">
        Tudo certo!
      </div>
    </div>
  </div>


  <hr>

  <div class="form-block">


  <div class="controla">  
  <div class="form-row">
    <div class="col-md-5 mb-3">
      <label for="validationCustom01">Nome do exercicio</label>
      <input type="text" class="form-control" id="validationCustom01" name="exercicio[]" placeholder="Nome do exercicio" required>
      <div class="valid-feedback">
        Tudo certo!
      </div>
    </div>
  </div>
</div>

  <div class="controla">
  <div class="form-row">
    <div class="col-md-3 mb-3">
      <label for="validationCustom02">Grupo Muscular</label>
      <select class="custom-select  mr-sm-2" id="inlineFormCustomSelectPref" name="grupoMusc[]">
    <option selected>Escolher...</option>
    <option value="biceps">Bíceps</option>
    <option value="triceps">Tríceps</option>
    <option value="costas">Costas</option>
  </select>
      <div class="valid-feedback">
        Tudo certo!
      </div>
    </div>

    <div class="col-md-3 mb-3">
      <label for="validationCustom02">Número de Séries</label>
      <select class="custom-select  mr-sm-2" id="inlineFormCustomSelectPref" name="numSeries[]">
    <option selected>Escolher...</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
  </select>
      <div class="valid-feedback">
        Tudo certo!
      </div>
    </div>

    <div class="col-md-3 mb-3">
      <label for="validationCustom02">Número de repetições</label>
      <select class="custom-select  mr-sm-2" id="inlineFormCustomSelectPref" name="numRep[]">
    <option selected>Escolher...</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
  </select>
      <div class="valid-feedback">
        Tudo certo!
      </div>
    </div>

    <div class="col-md-2 mb-3">
     <label for="validationCustom01">Peso</label>
      <input type="text" class="form-control" id="validationCustom01" name="peso[]" placeholder="Peso" required>
      <div class="valid-feedback">
        Tudo certo!
      </div>
    </div>

    <div class="col-md-1 ">
      <a class="remDiv" id="remover">Remover</a>
    </div>

  </div>
  <hr><hr>
  </div> 
</div>
<a class="btn btn-primary addDiv" style="color:white;">Add</a>&nbsp;
<input type="submit" name="btn-cadastrar" class="btn btn-primary">
</div>
</form>

2 answers

2


You can loop when inserting using for:

if(isset($_POST['btn-cadastrar'])){

$nomeFicha = $_POST['nomeFicha'];
$nomeExercicio = $_POST['exercicio'];
$grupoMuscular = $_POST['grupoMusc'];
$numSeries =  $_POST['numSeries'];
$numRepeticoes =  $_POST['numRep'];
$peso=$_POST['peso'];

for ($i=0; $i < count($nomeFicha); $i++) {        

    $sql = "INSERT INTO ficha (nomeFicha, nomeExercicio, grupoMuscular, numSeries, numRepeticoes, peso) VALUES('$nomeFicha[$i]', '$nomeExercicio[$i]', '$grupoMuscular[$i]', '$numSeries[$i]', '$numRepeticoes[$i]', '$peso[$i]')";

}

}

0

Try this way with foreach:

<?php
if(isset($_POST['btn-cadastrar'])){
  $dados = filter_input_array(INPUT_POST);
  $count= 0;

  foreach($dados['nomeFicha'] as $ficha){

     $nomeFicha = $ficha;
     $nomeExercicio = $dados['exercicio'][$count];
     $grupoMuscular = $dados['grupoMusc'][$count];
     $numSeries =  $dados['numSeries'][$count];
     $numRepeticoes =  $dados['numRep'][$count];
     $peso=$dados['peso'][$count];

     $count++;

     $sql = "INSERT INTO ficha (nomeFicha, nomeExercicio, grupoMuscular, numSeries, numRepeticoes, peso) VALUES('$nomeFicha', '$nomeExercicio', '$grupoMuscular', '$numSeries', '$numRepeticoes', '$peso')";

  }
}
  • thanks for the reply too, vlw.

Browser other questions tagged

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