0

I am doing a program with PHP but this is the first time I need to enter many arrays in a table. In the input I must place [] but when making the query insert how should I do? What I present in the code only saves me the last value without the [], and in the console, it passes the values ​​that I need, but it only inserts the last value.

<form class="" action="asignar-fechas.php" method="post">
  <?php foreach ($infoGrupo2 as $iGrupo2){
  if(($iGrupo2['fechaInicio']==$fechaA['numero_fecha'])){
      ?>
        <input type="hidden" name="id_grupo2[]" value="<?php echo $iGrupo2['id_grupo'];?>">
        <input type="hidden" name="modo2[]" value="<?php echo $iGrupo2['modo'];?>">
        <input type="hidden" name="fecha2[]" value="<?php echo $iGrupo2['fechaInicio'];?>">
          <input type="hidden" name="participante[]" value="<?php echo $iGrupo['participante'];?>">
          <input type="hidden" name="jugador<?php echo $juga;?>[]" value="<?php echo $in2['id_users'];?>">
}}?>
<input type="submit" name="enviar" value="ASIGNAR FECHAS">

asignar-fechas.php

   $id_grupo = $_POST['id_grupo2'];
  $modo = $_POST['modo2'];
  $fecha = $_POST['fecha2'];
  $participante = $_POST['participante'];
  $j1 = $_POST['jugador1'];
  $j2 = $_POST['jugador2'];
  $j3 = $_POST['jugador3'];
  $j4 = $_POST['jugador4'];

$insertarF = "INSERT INTO fechaxgrupo (grupo, fecha,estado) VALUES (:grupo, :fecha,0)";
$insertF = $conn->prepare($insertarF);
$insertF->bindParam(':grupo', $id_grupo);
$insertF->bindParam(':fecha', $fechaa);
$insertF->execute();

1 Answer 1

3

The $_POST variables elements will be arrays, you can loop over them. You need to bind to the iteration variables, not the arrays.

$insertarF = "INSERT INTO fechaxgrupo (grupo, fecha,estado) VALUES (:grupo, :fecha,0)";
$insertF = $conn->prepare($insertarF);
$insertF->bindParam(':grupo', $id_grupo);
$insertF->bindParam(':fecha', $fecha);
foreach ($_POST['id_grupo2'] AS $i => $id_grupo) {
    $fecha = $_POST['fecha2'][$i];
    $insertF->execute();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.