Write multiple records from one table to another with php

0

I have two tables, a schedule with schedules and a related schedule call.

tabela escala
id_escala
hr_inicio

tabela agenda
id_agenda 
id_escala
hr_agendamento

I want to record in the hr_address field in the scheduler table all the times in the scale table that I list. I'm able to write the id perfectly, but the times are being written so 00:00:00 .

My code:

<?php include("conectar.php");?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
    if (isset($_POST['enviar'])){       

        foreach($_POST['id_escala'] as $indice => $valor){
        $hr_agendamento   = $_POST['hr_agendamento'.$valor];


            $inserir = "INSERT INTO agenda
            (id_escala, hr_agendamento) VALUES ('".$valor."','".$hr_agendamento."')" 
            or die(mysql_error());
            mysql_query($inserir) or die(mysql_error());
        }
    }   
    // consulta do select de Serviços
    $selec = "SELECT * FROM escala limit  2";
    $exec = mysql_query($selec) or die(mysql_error());
    // Lista dados do checkbox 
?>

<form id="form1" action="" enctype=" multipart/form-data" method="post">
<?php
    while($dados = mysql_fetch_assoc($exec)){
        $valor_id_escala        =  $dados['id_escala'];

        $valor_hr_inicio     =  $dados['hr_inicio'];

?>
    <input style="margin-left:30px" name="id_escala[]" type="checkbox" value="<?php echo $valor_id_escala ?>"/>




    <input type="hidden" name="hr_agendamento" value="<?php echo $valor_hr_inicio ?>" />
    &nbsp;&nbsp;<?php echo $valor_hr_inicio ?></br>
<?php 
    } 
?>

    <input type="submit" name="enviar" value="gravar" />
</form>
    
asked by anonymous 20.08.2017 / 08:47

1 answer

0

This input

<input type="hidden" name="hr_agendamento" value=......

should have name this way name="hr_agendamento[]"

  

These brackets treat elements of the same name as an array.

  • Replace this code snippet:

    if (isset($_POST['enviar'])){ 
      //array com os elementos dos inputs hr_agendamento
      $hr_agend = $_POST['hr_agendamento'];      
    
      foreach($_POST['id_escala'] as $indice => $valor){
    
        $hr_agendamento=$hr_agend[$indice];
    
        $inserir = "INSERT INTO agenda
        (id_escala, hr_agendamento) VALUES ('".$valor."','".$hr_agendamento."')" 
        or die(mysql_error());
        mysql_query($inserir) or die(mysql_error());
      }
    }
    
20.08.2017 / 19:50