Problem when inserting. Insert the same record three times!

0

I am inserting data in a table called payments, the table stores payments that a student has made, the problem arises that when a student enrolls for the first time to process the first payment in the payment table, two rows are saved with all the empty fields and already in the 3rd row is that it saves the data well. but that only happens when it is the first payment of a new student, if I return to process another payment to that same student if he keeps the data well. In the form in which I send the data, some inputs are filled with a query. I will leave the function where I insert.

public function pago_cuota($id,$f_pay,$concepto,$monto,$forma,$bco,$refer)
  {
    $mysqli = new mysqli('localhost','root', '' , 'academia');

    $estatus = "PAGADA";
    $fecha_new = date('d-m-Y', strtotime("$f_pay + 1 month"));

    $query = "INSERT INTO pagos(id_student,concepto,monto,forma_pago,banco,n_referencia,fecha_pago,estatus)
    VALUES('$id','$concepto','$monto','$forma','$bco','$refer', '$fecha_new','$estatus')";
    $sql = $mysqli->query($query);
  }
    
asked by J. Castro 11.08.2017 в 01:01
source

1 answer

1

Try this:

Connection:

<?php

    $mysqli=new mysqli("localhost","root","","colegio"); //servidor, usuario de base de datos, contraseña del usuario, nombre de base de datos

    if(mysqli_connect_errno()){
        echo 'Conexion Fallida : ', mysqli_connect_error();
        exit();
    }
?>

Form that you add:

//  <body
<form method="post" action="" >
   <input name="cod">
   <input name="nom">
   <input name="apelli">
   <input name="ed">
   <input type="submit" name="submit" value="insertar">
</form>

<?php
    if(isset($_POST['submit'])){
        require("insertar.php");
    }
?>

// /body

Enter the data with the function:

<?php
$codd=$_POST['cod'];
$apelll=$_POST['nom'];
$nomm= $_POST['apelli'];
$edd=$_POST['ed'];

require("conexion.php");
if($codd>0){

    require("conexion.php");
    $sql = "INSERT INTO alumnos (id, nombre, apellidos, edad) 
            VALUES ('".$codd."','".$apelll."','".$nomm."','".$edd."');";

    $query_new_user_insert = mysqli_query($mysqli,$sql);
    echo 'Se ha registrado con exito';
    echo ' <script language="javascript">alert("Usuario registrado con éxito");</script> ';


}else{
    echo ' <script language="javascript">alert("deben ingresar un codigo");</script> ';
}

?>
    
answered by 11.08.2017 в 01:29