0

Trying to integrate a new script with my existing homepage I am required to include 2 new php files in my index.php. I love playing with web development but sadly my coding knowlage is very limited. (CMS and GUI make human stupid)

if ($page == 'Premium') {
    include('affiliates/controller/affiliate-tracking.php');

    if (isset($_GET['payment']) && $_GET['payment'] == 'created') {
        $sale_amount = '5.00';
        $product = 'Premium';

        include('affiliates/controller/record-sale.php');

    }
}

If I do this I get the fatal error: Call to a member function fetch_assoc() on a non-object in affiliates/controller/record-sale.php on line 61.

Code in line 61 is this:

$stmt->execute();

Can someone Point me in the right direction here? I am not really sure what the error means. There don't seem to be any database errors and all the other functions work the way they are supposed to.

Thanks to any samaritan who answers.

Using mysqli complete code for record-sale.php is this:

application/x-httpd-php record-sale.php ( PHP script text )

<?php
$path = dirname(__FILE__);
$path = substr($path, 0, -10);
include($path.'/auth/db-connect.php');
$ap_tracking = 'ap_ref_tracking';
$datetime = date("Y-m-d H:i:s");

//IF AFFILIATE ID IS PRESENT
if(isset($_COOKIE[$ap_tracking])){
    session_start();

    //GET COMMISSION LEVEL
    $get_dc = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT default_commission FROM ap_settings WHERE id=1"));
    $default_commission = $get_dc['default_commission'];

    //IS SALE VOLUME COMMISSIONS ON
    $get_sv = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT sv_on FROM ap_other_commissions WHERE id=1"));
    $sv_on = $get_sv['sv_on'];

    if($sv_on=='1'){
    $get_cl = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT percentage FROM ap_commission_settings WHERE $sale_amount BETWEEN sales_from AND sales_to"));
    $comission = $get_cl['percentage'];
    }

    //COMMISSION OVERRIDE
    $comission = $commission;

    //SET DEFAULT COMMISSION IF NO OTHER VALUE HAS BEEN SET
    if($comission==''){$comission = $default_commission;}

    //RECURRING COMMISSIONS
    if(isset($recurring)){
        $recurring_period = strtolower($recurring);
        $recurring_amount = $recurring_fee;
        if($recurring_period==''){$recurring_period='monthly';}
        if($recurring_fee==''){$recurring_fee = $default_commission;}
    }else{
        $recurring_period = 'Non-recurring';
        $recurring_fee = '0';
    }

    //RECORD SALE
    $affiliate_id = $_COOKIE[$ap_tracking];
    $percentage = $comission / 100;
    $net_earnings = $sale_amount * $percentage;
    $datetime = date("Y-m-d H:i:s");

    //CHECK FOR VALID AFFILIATE
    $get_affiliate = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT balance FROM ap_members WHERE id=$affiliate_id"));
    $affiliate_balance = $get_affiliate['balance'];

    if(isset($affiliate_balance)){
        //PREVENT DUPLICATE SALES
        if($_SESSION['ap_sale_hit']=='1' && $affiliate_id == $_SESSION['saved_affiliate'] && $net_earnings == $_SESSION['saved_net']){}else{
        $stmt = $mysqli->prepare("INSERT INTO ap_earnings (affiliate_id, product, comission, sale_amount, net_earnings, recurring, recurring_fee, datetime) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
        $stmt->bind_param('ssssssss', $affiliate_id, $product, $comission, $sale_amount, $net_earnings, $recurring_period, $recurring_fee, $datetime);
        $stmt->execute();
        $transaction_id = $stmt->insert_id;
        $stmt->close();
        //UPDATE AFFILIATE BALANCE
        $updated_balance = $affiliate_balance + $net_earnings;
        $update_one = $mysqli->prepare("UPDATE ap_members SET balance = ? WHERE id=$affiliate_id"); 
        $update_one->bind_param('s', $updated_balance);
        $update_one->execute();
        $update_one->close();   

        }
    }

    //MULT-TIER COMMISSIONS
    $get_mt = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT mt_on FROM ap_other_commissions WHERE id=1"));
    $mt_on = $get_mt['mt_on'];
    if($mt_on=='1'){
        $levels = 10;
        //FOR EACH LEVEL RUN
        $get_sponsor = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT sponsor FROM ap_members WHERE id=$affiliate_id"));
        $sponsor = $get_sponsor['sponsor'];
        for ($loop = 2 ; $loop < $levels; $loop++){ 

            //CHECK FOR AVAILABLE SPONSOR
            if($sponsor!='0'){

                //GET LEVEL PERCENTAGE
                $gp = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT * FROM ap_other_commissions WHERE id=1"));
                $p = $gp['tier'.$loop.'']; 
                $sc = $p / 100;
                $se = $sale_amount * $sc;

                //GET SPONSOR BALANCE
                $gb = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT balance FROM ap_members WHERE id=$sponsor"));
                $ob = $gb['balance']; 
                $nb = $ob + $se;
                $update_one = $mysqli->prepare("UPDATE ap_members SET balance = ? WHERE id=$sponsor"); 
                $update_one->bind_param('s', $nb);
                $update_one->execute();
                $update_one->close();

                    //INSERT TRANSACTION HISTORY
                    $stmt = $mysqli->prepare("INSERT INTO ap_multi_tier_transactions (affiliate_id, transaction_id, tier, commission, mt_earnings, datetime) VALUES (?, ?, ?, ?, ?, ?)");
                    $stmt->bind_param('ssssss', $sponsor, $transaction_id, $loop, $p, $se, $datetime);
                    $stmt->execute();
                    $stmt->close();
            }else{
                break 1;
            }
            //GET NEXT SPONSOR FOR NEXT LOOP
            $gs = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT sponsor FROM ap_members WHERE id=$sponsor"));
            $sponsor = $gs['sponsor'];
        }
    }

    $_SESSION['ap_sale_hit'] = '1';
    $_SESSION['saved_affiliate'] = $affiliate_id;
    $_SESSION['saved_net'] = $net_earnings;

}

if(isset($_POST['reset'])){
unset($_SESSION['ap_sale_hit']);
unset($_SESSION['saved_affiliate']);
unset($_SESSION['saved_net']);
//header("Refresh:0");
}
?>
cam
  • 13
  • 5
  • http://php.net/manual/en/function.error-reporting.php and not enough code here. Plus, no idea which MySQL API you're using here. – Funk Forty Niner Feb 01 '16 at 22:10
  • You should include more of that `record-sale.php` file, we can't tell what's wrong just from one line – Voreny Feb 01 '16 at 22:10
  • The function fetch_assoc() is included in the record-sale.php. A function call is executed on something that is supposed be an object. – bestprogrammerintheworld Feb 01 '16 at 22:11
  • Please supply us with the record-sale.php (at least 10 rows or something that refers to the database) so we can help you. – bestprogrammerintheworld Feb 01 '16 at 22:19
  • 1
    @devpro Probably C) => `mysql_` - who knows. they're not answering and expecting a magic answer. Won't happen. *ciao for now* – Funk Forty Niner Feb 01 '16 at 22:21
  • No no I pray not option C .. Ciao ahan I understand your encoded words bro @fred-ii – devpro Feb 01 '16 at 22:24
  • I've added the complete record-sale.php code now. Sorry – cam Feb 01 '16 at 22:40
  • There's no way that error message can come from the `$stmt->execute()` line. It has to come from a line that contains `fetch_assoc`. – Barmar Feb 01 '16 at 22:43
  • You should split up your `mysqli_query` and `mysqli_fetch_assoc` calls into two statements, so you can check whether `mysqli_query` is successful first. – Barmar Feb 01 '16 at 22:43
  • Use `$result = mysqli_query(...) or die(mysqli_error($mysqli)); $get_sponsor = mysqli_fetch_assoc($result);` – Barmar Feb 01 '16 at 22:44
  • the error is: Fatal error: Call to a member function fetch_assoc() on a non-object in /home/omegaload/public_html/files/index.php on line 61 snd line 61 is $stmt->execute(). line 51 contains this: $get_affiliate = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT balance FROM ap_members WHERE id=$affiliate_id")); – cam Feb 01 '16 at 22:46

0 Answers0