problem with a query

0

I have a kind of invoice in which a calculation is made of the user that loguea of how much should be paid in that month of invoice (is condominium) in the query where the calculation is done I add that you make insert data in a table to save the amount generated from that user in the month, but I have a problem that every time that user enters the module or page where the invoice is displayed is added to the table said amount, that is if it enters 10 times the page is added 10 times the data to the table, what I want to do is to add only 1 time per month (since the bill is monthly) no matter how many times you enter the page, this is the code ... referring to the part of the insert into receipt

 <?php
                        error_reporting(E_ALL^E_NOTICE);

  $ip_global = $_SERVER['REMOTE_ADDR'];
  date_default_timezone_set('America/Guyana');
  $global_fecha = date("d-m-y H:i:s");
 require_once('../conexion.php');

 $alicuota= $_SESSION['alicuota'];
   $sql = "SELECT SUM( monto ) as total
FROM gasto_g
WHERE YEAR( create_at ) = YEAR( CURRENT_DATE( ) ) 
AND MONTH( create_at ) = MONTH( CURRENT_DATE( ) ) 
ORDER BY create_at ASC"; 
   $Resultado = mysqli_query ($conexion,$sql); 
    while ($Datos = mysqli_fetch_array($Resultado)){
      
      $alicuota= $_SESSION['alicuota'];
            $total1=($Datos['0'] * $alicuota)/100;

 $id= $_SESSION['id'];
            $consulta = "INSERT INTO recibo VALUES('','$total1','$id')";

$query = mysqli_query($conexion, $consulta);
if ($query) {
  ?>
  
  <?php
}
    
?>
    
asked by Anderson Rey 07.10.2017 в 03:20
source

2 answers

0

You could do a search in the recibo table and if you do not find any rows for that user, then just run the insert .

Something like that (I am inventing the names of fields, I do not know if they are called user_id and date):

$id = $_SESSION['id'];
$mes = date('m');
$anio = date('Y');

$query = "SELECT * 
          FROM recibo 
          WHERE usuario_id = $id
          AND YEAR(fecha) = $anio
          AND MONTH(fecha) = $mes";

$result = mysqli_query($conexion, $query);

// si no se encontró nada para ese usuario...
if (mysqli_num_rows($result) == 0) {
    // ... calcular y ejecutar INSERT
}

Another option could be to use a cronjob on the server, which runs once a month a script to calculate the amounts of all users. Then when a user loguea no longer need to do the calculation at that moment.

On the servers you can schedule tasks, in the case of Linux this is done with a cronjob command. It basically consists of indicating the path of a file to be executed and what date and time it should be executed. In your case, it would be a PHP file that monthly generates all the inserts in the table recibo .

    
answered by 07.10.2017 / 03:31
source
0

In my personal opinion the best thing is to create a primary key, so that you have integrity in your system, and later you generate the validation you can use an if exists to validate if the registry exists and then insert it I would generate a store procedure and my validation inside this.

    
answered by 07.10.2017 в 04:45