Write multiple records from one table to another with php

Asked

Viewed 108 times

0

I have two tables, a scale with schedules and a related one called agenda.

tabela escala
id_escala
hr_inicio

tabela agenda
id_agenda 
id_escala
hr_agendamento

I want to record in the hr_schedule field in the schedule table all times of the scale table I list. I am managing to record the id perfectly, but the times are being recorded like this 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>
  • what kind of data in the hr_start and hr_scheduling column?

1 answer

0


This input

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

should have the name thus 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());
      }
    }
    
  • thank thank it worked Ta working now

  • sorry I’m new here I’m already reading thank you.

Browser other questions tagged

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