Calculate boleto/parcel in PHP

Asked

Viewed 852 times

1

I have an HTML form that, after being filled in, the values of the fields are sent via POST method to a PHP file and this file captures these values by sending to the Mysql database.

My problem lies in the calculation of installment accounts. If I make a purchase in installments in 5 times, for example, in the form I put the account data, the amount of installments and the first maturity. PHP takes the first maturity (date) and adds up to 4 further installments. That is, if the first installment is day 05/06/2017, the next 4 installments would be: 05/07, 05/08, 05/09 and 05/10/2017. The problem that when executing the form, it returns a blank page and does not save anything in Mysql. Connections are correct, there is return of PHP errors in case of code error and etc.

Follow the stretch for assistance:

if($parcelado == "Sim") {

    $opcoes = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8');
    $conexao = new PDO("mysql:host=".SERVER."; dbname=".DBNAME, USER, PASSWORD, $opcoes);

    for($x = 0; $x < $nParcela; $x++){

        $timestamp = strtotime("+ $nParcela month");
        //$date('d/m/Y H:i', $timestamp);

        $sql  = "INSERT INTO boleto(nome_boleto,data_inclusao,vencimento,valor_boleto,descricao,pago,nParcela, dataparcela) VALUES ('$boleto', '$dtpgto', '$timestamp', '$pagamento', '$descricao', '$pago', '$nParcela')";

        $stm = $conexao->prepare($sql);
        $stm->execute();
    }

}   

In the form have a field (Installments?) if yes, enter this condition, if not enter another.

Follows the variables:

$dbpgto1 = $_POST['payment'];
$dtvencimento1 = $_POST['maturity'];
$boleto = $_POST['invoice'];
$dtpgto = date("Y-m-d",strtotime(str_replace('/','-',$dbpgto1)));
$dtvencimento = date("Y-m-d",strtotime(str_replace('/','-',$dtvencimento1)));
$pagamento = $_POST['value'];
$mensal = $_POST['radiosmensal'];
$descricao = $_POST['textarea'];
$nParcela = $_POST['nParcela'];  
$dataParcela = $_POST['dataparcela'];    
$pago = "Nao";
#$dataPrimeiraParcela = $_POST['dataparcela'];
#$nParcelas = $POST_['nParcela'];
$parcelado = $_POST['radiosparcelado'];

Finally, if I put one echo at the end of for, he returns what I put in him.

  • Within the for, try to put if(!$stm) { die($conexao->errorInfo()); } to see if there are any mistakes.

  • the return of strtotime returns a timestamp. If maturity is of the date type you will need to convert the timestamp in the correct format. Using $timestamp = date ('Y-m-d',$timestamp );

1 answer

1


You came to analyze the return of all variables of what goes to the database

Type you designed 8 columns but only inserting 7.

$date1 = explode("/", $dbpgto1);
$date2 = explode("/", $dtvencimento1);
$dtpgto = date("Y-m-d",mktime(0, 0, 0, $date1[1], $date1[0], $date1[2]));
$parcela = 7;

for($i=0; $i <= $parcela; $i++){

    $nParcela = $i;

    $dtvencimento = date("Y-m-d",strtotime("+".$i." Months",mktime(0, 0, 0, $date2[1], $date2[0], $date2[2])));

    $sql  = "INSERT INTO boleto(nome_boleto,data_inclusao,vencimento,valor_boleto,descricao,pago,nParcela, dataparcela) 
             VALUES ('$boleto', '$dtpgto', '$timestamp', '$pagamento', '$descricao', '$pago', '$nParcela', **'$dataParcela'**)";

  }
  • I checked and in fact was wrong the number of columns with the Values to the Mysql database. I did a quick test and recorded the data, but the date came as 0000.00.00. I think that’s what @Everton said above about the conversion. I’ll take my time.

  • See if now it helps you. If it helps you don’t forget to mark the answer. :)

  • It was created the dates repeatedly, that is, if I have 7 installments, the due date had 15/06/2017 seven times and what I need is the first is 15/06/2017 and then 15/07, 15/08, 15/09 and etc.

  • Take a look now.

  • It worked, partner. Thank you :)

Browser other questions tagged

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