0

I wrote some code to check database table name is hms_history if type = commission then will add another row of data into hms_history but change amount_balance to -amount_balance then will add another data to table hms_deposit. I try to write the code but when I check it show only connect successful, but nothing changes in the database at all.

This is my code

<?php
$servername = "localhost";
$username = "xxxx";
$password = "xxxx";

function setChecked($conn,$params) {
    $s = $conn->prepare("UPDATE `hm2_history`
            SET ref_id=-1
            WHERE id=:id
        ");
    $s->execute($params);
}

try {
    $conn = new PDO("mysql:host=$servername;dbname=xxxx", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
}
catch(PDOException $e)
{
    echo "Connection failed: " . $e->getMessage();
}

    $stmt = $conn->prepare("SELECT * FROM hm2_history 
        WHERE id NOT IN(SELECT ref_id FROM hm2_history WHERE ref_id > 0) AND 
        ref_id=0 AND commission=0");

    //deposits
    $s = $conn->prepare("INSERT INTO `hm2_deposits`
        SET `user_id`=:user_id,
        `type_id`=:type_id,
        `deposit_date`=:deposit_date,
        `last_pay_date`=:last_pay_date,
        `status`=:status,
        `q_pays`=:q_pays,
        `amount`=:amount,
        `actual_amount`=:actual_amount,
        `ec`=:ec,
        `compound`=:compound,
        `dde`=:dde,
        `unit_amount`=:unit_amount,
        `bonus_flag`=:bonus_flag,
        `init_amount`=:init_amount,
        `ref_id`=:ref_id
    ");

            $v['type_id']= 9; 
            unset($v['id']);

        $lastDepositId = $conn->lastInsertId();

            $date = date('Y-m-d H:i:s');
            $s->execute($v);

            //history1
            $s = $conn->prepare("INSERT INTO `hm2_history`
                SET `user_id`=:user_id,
                `amount`=:amount,
                `type`=:type,
                `description`=:description,
                `actual_amount`=:actual_amount,
                `date`=:date,
                `ec`=:ec,
                `deposit_id`=:deposit_id,
                `rate`=:rate
            ");
            $x = array(
                'user_id'=>$v['user_id'],
                'type'=>'bonus',
                'amount'=>$v['amount'],
                'description'=>'Bonus note',
                'actual_amount'=>$v['actual_amount'],
                'date'=>$date,
                'ec'=>68,
                'deposit_id'=>0,
                'rate'=>1.0,
            );
            $s->execute($x);

            //history2
            $s = $conn->prepare("INSERT INTO `hm2_history`
                SET `user_id`=:user_id,
                `amount`=:amount,
                `type`=:type,
                `description`=:description,
                `actual_amount`=:actual_amount,
                `date`=:date,
                `ec`=:ec,
                `deposit_id`=:deposit_id,
                `rate`=:rate
            ");
            $x = array(
                'user_id'=>$v['user_id'],
                'type'=>'depoit',
                'amount'=> -$v['amount'],
                'description'=>'Deposit from plan',
                'actual_amount'=> -$v['actual_amount'],
                'date'=>$date,
                'ec'=>999,
                'deposit_id'=>$lastDepositId,
                'rate'=>1.0,
            );
            $s->execute($x);

?>
marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
bwnm29
  • 19
  • 3
  • Some improved formatting. – wazz Oct 20 '19 at 23:09
  • Good work setting PDO to throw exceptions but have you made sure you can see them? See [How can I get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-can-i-get-useful-error-messages-in-php) – Phil Oct 20 '19 at 23:14
  • FYI, `$lastDepositId = $conn->lastInsertId();` won't return anything useful as you have not executed your `INSERT` statement at that time. Also, `$v` doesn't appear to be defined – Phil Oct 20 '19 at 23:15

0 Answers0