1

I already write the code to check if table call hm2_history type = commission if yes then insert data into table call hm2_deposit, when I test echo was correct and show the result is :

Connected successfully
354
368

But won't insert into hm2_deposit , I don't know how to adjust it i have a little bit knowledge about php

This is my code

<?php
$servername = "localhost";
$username = "tinybaht_findroom";
$password = "212224";


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

try {
    $conn = new PDO("mysql:host=$servername;dbname=tinybaht_findroom", $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");
    $stmt->execute();
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $a = $stmt->fetchAll();
    $plans = array();
    foreach($a as $i){
        $plans[$i['type']] = 'commissions';
    }

    $stmt = $conn->prepare("SELECT * FROM hm2_history WHERE id NOT IN(SELECT ref_id FROM hm2_deposits WHERE ref_id > 0)  AND type='commissions'");
    $stmt->execute();
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $rows = $stmt->fetchAll();
    foreach($rows as $k=>$v) {


        $plan_type = isset($plans)?:'';
        $m = $v['type'];
        if (!empty($plan_type)){
            echo '<br>'.$v['id'];


                if ($m = "commissions" ){
                    setChecked($conn,array('id'=>$v['id']));
                    continue;
                }
            }else{
                setChecked($conn,array('id'=>$v['id']));
                continue;
            }

            //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['ref_id'] = $v['id'];
            $v['amount'] = $v['amount']*$rate;
            $v['actual_amount'] = $v['actual_amount']*$rate;
            $v['init_amount'] = $v['init_amount']*$rate;
            $v['bonus_flag'] = 1;
            $v['type_id']= 9; 
            unset($v['id']);
        $s->execute($v);
        $lastDepositId = $conn->lastInsertId();

            $date = date('Y-m-d H:i:s');
    }


?>

this is photo of my db table name is hm2_deposits hm2_deposits

this is photo of my db table name is hm2_history enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
bwnm29
  • 19
  • 3

1 Answers1

0

There is an error in your SQL: $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 "); Read the proper way to do it at: https://www.w3schools.com/sql/sql_insert.asp