Save an array from the form to the database

Asked

Viewed 518 times

1

I have tried several ways to save the data that comes in the form in the database but it is not working, when using PHP5 worked but now converting to PHP7 stopped working, someone can 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 not in a certain way, it says that I can not access the POST directly. I can’t even get the data.

<?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'];
?>

1 answer

1

I haven’t programmed in php for a long time, but an idea would be to make a FOR for each request and save 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>';
    }
?>
  • It’s not right, Because it has a radius of a filter against mysqli Injection that won’t let it come.

  • I don’t understand what you want, a.way to bypass the system or just make a request?

  • Dear Colleague, I’m trying to get the data sent by the form and save in the database. The form adds the lines according to the quantity of medicine to be ordered via javascript, so that’s fine. When I make a file to send to the database it does not send. If I use the direct code to pick up the contents of the array it says I cannot directly access the data of the array, I need a filter. I’m unable to filter the data in the array in php 7 .

Browser other questions tagged

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