Save an array from the form in the database

0

I have tried in several ways to save the data that comes in the form in the database but it is not working, when I used PHP5 it worked but now converting to PHP7 stopped working, can anyone help me.

The Registration form:

<?php
if (!isset($seguranca)) {
    exit;
}
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);

$result_paciente = "SELECT * FROM pacientes WHERE id = '$id'";
$resultado_paciente = mysqli_query($conn , $result_paciente);

?>
<div class="well conteudo">

 <div class="container" style="background-color:#efefef">
  <center><h2>Receituario</h2></center>
   <?php while($row_paciente = mysqli_fetch_assoc($resultado_paciente)){?>
    <table class="table">
      <tr>
          <td>
            <label>Paciente</label>
              <td class="text-left" class="form-control" name="paciente"><?php echo $row_paciente["nome_paciente"]; ?></td>
          </td>
          <td>
            <label>Profissional</label>
          <td class="text-left" class="form-control" name="user"> <?php echo $_SESSION['nome'];?> </td>
            <?php } ?>      
          </td>
      </tr>
    </table>

     <form action="<?php echo pg; ?>/processa/proc_cad_receitas" method="POST" class="form-horizontal">
    <hr style="border:1px dashed black">
    <table class="table table-hover">
      <thead>
        <tr>
          <th>Medicamento</th>
          <th>Qtd Comprar</th>
          <th>Qtd Tomar</th>
          <th>Intervalo</th>
          <th>Nº Dias</th>
          <th><input type="button"  class="btn btn-primary add" value="+"></th>
        </tr>
      </thead>
      <tbody class="details">
          <tr>
               <td><select type="text" name="med[]" class="form-control med"> <option value=""></option>
              <?php
                $result_medicamento = "SELECT M.medicacao_id, M.comercial, V.nome_via, A.nome_apre
                                              FROM tbl_drugs_medicacao AS M
                                              INNER JOIN tbl_drugs_vias_cadastradas AS V
                                              ON M.via_id = V.id_via
                                              INNER JOIN tbl_drugs_apresentacao AS A
                                              ON M.apresentacao_id = A.id_apre
                                              ORDER BY comercial";
                $resultado_medicamento = mysqli_query($conn, $result_medicamento);
                while($row_medicamento = mysqli_fetch_assoc($resultado_medicamento) ) {
                  echo '<option value="'.$row_medicamento['medicacao_id'].'">'.$row_medicamento['comercial'].'-|-'.$row_medicamento['nome_via'].'-|-'.$row_medicamento['nome_apre'].'</option>';
                  }
              ?>
              </select></td>
               <td><input type="text" name="compra[]" class="form-control compra"></td>
               <td><input type="text" name="tomar[]" class="form-control tomar"></td>
               <td><select type="text" name="inter[]" class="form-control inter"><option value=""></option>
                <?php
                  $result_intervalo = "SELECT * FROM tbl_drugs_intervalo";
                  $resultado_intervalo = mysqli_query($conn, $result_intervalo);
                  while($row_intervalo = mysqli_fetch_assoc($resultado_intervalo) ) {
                    echo '<option value="'.$row_intervalo['id_inter'].'">'.$row_intervalo['nome_inter'].'</option>';
                  }
                ?>
              </select></td>
               <td><select type="text" name="day[]" class="form-control day"><option value=""></option>
                <?php
                $result_dia = "SELECT * FROM tbl_drugs_dias";
                $resultado_dia = mysqli_query($conn, $result_dia);
                while($row_dia = mysqli_fetch_assoc($resultado_dia)){
                  echo '<option value="'.$row_dia['id_dias'].'">'.$row_dia['nome_dias'].'</option>';
                  }
                ?>
              </select></td>
               <td><input type="button"  class="btn btn-danger remove" value="Remover"></td>
          </tr>
      </tbody>
      <tfoot>
        <tr>   
            <td></td>
            <td></td>
            <td></td>
            <td></td> 
            <td></td> 
            <td></td> 
            <td>

            <input type="submit" class="btn btn-success" name="Sendreceitas" value="Salvar">
          </td>
        </tr>
      </tfoot>
    </table>
  </form>
 </div>

</body>
</html>


<script type="text/javascript">


    $(function(){


    // add new row 
    $('.add').click(function(){
      var tr = '<tr>'+
                   '<td><select type="text" name="med[]" class="form-control med"> <option value=""></option><?php
                $result_medicamento = "SELECT M.medicacao_id, M.comercial, V.nome_via, A.nome_apre
                                              FROM tbl_drugs_medicacao AS M
                                              INNER JOIN tbl_drugs_vias_cadastradas AS V
                                              ON M.via_id = V.id_via
                                              INNER JOIN tbl_drugs_apresentacao AS A
                                              ON M.apresentacao_id = A.id_apre
                                              ORDER BY comercial";
                $resultado_medicamento = mysqli_query($conn, $result_medicamento);
                while($row_medicamento = mysqli_fetch_assoc($resultado_medicamento) ) {
                  echo '<option value="'.$row_medicamento['medicacao_id'].'">'.$row_medicamento['comercial'].'-|-'.$row_medicamento['nome_via'].'-|-'.$row_medicamento['nome_apre'].'</option>';
                  }
              ?></select></td>'+
                   '<td><input type="text" name="compra[]" class="form-control compra"></td>'+
                   '<td><input type="text" name="tomar[]" class="form-control tomar"></td>'+
                   '<td><select type="text" name="inter[]" class="form-control inter"><option value=""></option>                <?php
                  $result_intervalo = "SELECT * FROM tbl_drugs_intervalo";
                  $resultado_intervalo = mysqli_query($conn, $result_intervalo);
                  while($row_intervalo = mysqli_fetch_assoc($resultado_intervalo) ) {
                    echo '<option value="'.$row_intervalo['id_inter'].'">'.$row_intervalo['nome_inter'].'</option>';
                  }
                ?></select></td>'+
                   '<td><select type="text" name="day[]" class="form-control day"><option value=""></option>               <?php
                $result_dia = "SELECT * FROM tbl_drugs_dias";
                $resultado_dia = mysqli_query($conn, $result_dia);
                while($row_dia = mysqli_fetch_assoc($resultado_dia)){
                  echo '<option value="'.$row_dia['id_dias'].'">'.$row_dia['nome_dias'].'</option>';
                  }
                ?></select></td>'+
                   '<td><input type="button"  class="btn btn-danger remove" value="Remover"></td>'+
              '</tr>';
      $('.details').append(tr);
    });
    // end 

     // delete row 
    $('.details').delegate('.remove','click',function(){
        var con = confirm("Deseja Realmente Excluir esse medicamento ?");
        if(con)
        {
          $(this).parent().parent().remove();
          total();
        }

    });
    // end 

  });
</script>


    </div>


Formulario Antigo do PHP 5 

<?php
  session_start();

  $id_prof = $_SESSION['usuarioId'];
  include_once("../../../../conexao/conexao.php");
?>

<?php
    $id = $_SESSION['paciente_receita'];
    $result_paciente = "SELECT * FROM tbl_pacientes WHERE id = '$id'";
    $resultado_paciente = mysqli_query($conn , $result_paciente);
    $id_receita = $_SESSION['receitas_emitir'];
 ?>

<?php 

        if(isset($_POST['order']))
          {   
            if($id > 0)
              {
                for($i=0;$i<count($_POST['med']);$i++)
                  {
        $med      = mysqli_real_escape_string($conn, $_POST['med'][$i]);
        $compra   = mysqli_real_escape_string($conn, $_POST['compra'][$i]);
        $toma     = mysqli_real_escape_string($conn, $_POST['tomar'][$i]);
        $inter    = mysqli_real_escape_string($conn, $_POST['inter'][$i]);
        $dias     = mysqli_real_escape_string($conn, $_POST['day'][$i]);

$result_itens = "INSERT INTO tbl_pacientes_receitas_itens(presc_id, med_id, compra, toma, inter_id, dias_id, criado) VALUES ('$id_receita', '$med', '$compra', '$toma', '$inter', $dias, NOW())";
  $resultado_itens = mysqli_query($conn, $result_itens);  

                  }
              }
          }
?>

<!DOCTYPE html>
<html lang="pt-BR">
    <head>
        <meta charset="utf-8">
    </head>

    <body>
        <?php
            if(mysqli_affected_rows($conn) != 0){
                header('location: http://localhost/clinica/adm/adm.php?link=41&id='.$id);  
                    }
                        unset($_SESSION["receitas_emitir"]); 
        ?>
    </body>
</html>
<?php $conn->close(); ?>

I tried to use this code but it does not work at all, it says that I can not access POST directly. Nor do I get the data I'm getting.

<?php

if (!isset($seguranca)) {
    exit;
}
$id = $_SESSION['id_pac'];

$prontuario_id = $_SESSION['prontuario'];

$result_paciente = "SELECT * FROM pacientes WHERE id = '$id'";
$resultado_paciente = mysqli_query($conn, $result_paciente);
$row_paciente = mysqli_fetch_assoc($resultado_paciente);

$receita = filter_input_array(INPUT_POST, FILTER_DEFAULT);

echo $receita['med'];
echo $receita['compra'];
echo $receita['tomar'];
echo $receita['inter'];
echo $receita['day'];
?>
    
asked by anonymous 09.01.2018 / 21:17

1 answer

0

I have not programmed in php for some time, but one idea would be to make a FOR for each request and save it inside the array, and in the same way you can use it to display. ex:

 <?php
    $results = array
      (
      array("Marcos","ritalina","18","2"),
      array("Julia","ibuprofeno","30","1"),
      array("Marcelo","alprazolam ","06","4"),
      array("Antonia","clonazepam ","02","2"),
      );


    for ($i = 0; $i < 4; $i++) {
        $j = 0;
        print_r('Paciente: '.$results[$i][$j].'. medicamento: '.$results[$i][$j+=1].'. dose(mg): '.$results[$i][$j+=1].'. vezes ao dia: '.$results[$i][$j+=1].'.' );
        echo '<br>';
    }
?>
    
09.01.2018 / 22:21