Problem with my PHP script [Need help]

Discussion in 'PHP' started by farouqxii, Jan 1, 2012.

  1. #1
    Hello community of DP.
    I'm currently working with my new website and was coding and fixing stuff until I noticed that the main thing for the website, the postback script which rewards the users is not working as it should.
    Here is the code:
    <?php
    session_start( );
    include_once( "includes/db_inc.php" );
    $mysql_where = $DB_HOST;
    $mysql_username = $DB_USER;
    $mysql_password = $DB_PASS;
    $mysql_database = $DB_NAME;
    mysql_connect( $mysql_where, $mysql_username, $mysql_password );
    mysql_select_db( $mysql_database );
    $result = mysql_query( "SELECT password FROM gateway WHERE id = '1'" );
    if ( !$result )
    {
        echo "Could not run query: ".mysql_error( );
        exit( );
    }
    $row = mysql_fetch_row( $result );
    $yourpassword = $_REQUEST['password'];
    if ( $password != $yourpassword )
    {
        $msg = "Somebody is trying to access the postback URL posting the invalid password.\nThe user IP is ".$_SERVER['REMOTE_ADDR'].".";
        mail( "admin@yiyd.com", "Invalid Postback Access", $msg );
        echo $msg;
        exit( );
    }
    else
    {
        $network_name = "Cpalead";
        $subid_var = "subid";
        $earn_var = "earn";
        $pdtshow_var = "pdtshow";
        $subid = trim( $_REQUEST[$subid_var] );
        $earn = trim( $_REQUEST[$earn_var] );
        $pdtshow = trim( $_REQUEST[$pdtshow_var] );
        $subid_parts = explode( "i", $subid );
        $stat_id = $subid_parts[1];
        $parts2 = explode( "f", $subid_parts[0] );
        $file_id = $parts2[0];
        $user_id = $parts2[1];
        $sql8 = mysql_query( "SELECT percent, affiliate_commission FROM referral_percentage WHERE id = '1'" );
        $row8 = mysql_fetch_array( $sql8 );
        $referral_percentage = $row8['percent'];
        $affiliate_percent = $row8['affiliate_commission'];
        $sql1 = mysql_query( "select conversion from conversions where id = '{$stat_id}'" );
        $row9 = mysql_fetch_array( $sql1 );
        $conversion = $row9['conversion'];
    }
    if ( $conversion == 1 )
    {
        echo "The same conversion can't be sent twice.";
        exit( );
    }
    else
    {
        $now = date( "Y-m-d H:i:s" );
        $sql2 = mysql_query( "update conversions set conversion = 1, conversion_time = '{$now}' where id='{$stat_id}'" );
        $sql4 = mysql_query( "select user_name, referrer_id from users where id = '{$user_id}'" );
        $row4 = mysql_fetch_array( $sql4 );
        $user_name = $row4['user_name']." (User ID: {$user_id})";
        $sql5 = mysql_query( "select file_title, file_name from file_uploader where upload_id = '{$file_id}'" );
        $row5 = mysql_fetch_array( $sql5 );
        $user_string = $user_name." had an offer completed while downloading file: ".$row5['file_name']." (".$row5['file_title']."). ";
        $referal_string = $user_name." had an offer completed while downloading file: ".$row5['file_name']." (".$row5['file_title']."). This added to the users referal earnings.";
        $credit_amount = round( $earn * $affiliate_percent, 2 );
        $admin_price = $earn;
        $referrer_id = $row4['referrer_id'];
    }
    if ( $referrer_id == 0 )
    {
        $sql7 = mysql_query( "insert into user_earnings set user_id = '{$user_id}', amount = '{$credit_amount}', description = '{$user_string}', earn_date = '{$now}', earn_type ='direct'" );
    }
    else
    {
        $referral_credit = round( $credit_amount * $referral_percentage, 2 );
        $user_credit = round( $credit_amount * ( 1 - $referral_percentage ), 2 );
        $sql9 = mysql_query( "insert into user_earnings set user_id = '{$user_id}', amount = '{$user_credit}', description = '{$user_string}', earn_date = '{$now}', earn_type ='direct'" );
        $sql10 = mysql_query( "insert into user_earnings set user_id = '{$referrer_id}', amount = '{$referral_credit}', description = '{$referal_string}', earn_date = '{$now}', earn_type ='referral'" );
    }
    $sql10 = mysql_query( "insert into admin_earnings set user_id = '{$user_id}', amount = '{$admin_price}', converted_date = '{$now}'" );
    echo "Success";
    echo "\r\n";
    ?>
    PHP:
    The problem is, the script ONLY excutes this part:
    $sql10 = mysql_query( "insert into admin_earnings set user_id = '{$user_id}', amount = '{$admin_price}', converted_date = '{$now}'" );
    echo "Success";
    echo "\r\n";
    PHP:
    I couldn't really figure it out, so if you could help could you please tell me how it should look like and why because I'm willing to learn :)

    Thanks in advance guys!
     
    farouqxii, Jan 1, 2012 IP
  2. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #2
    For the sake of debugging, try something like this:
    $sql = "your query here";
    $result = mysql_query($sql) or die($sql . "\n" . mysql_error());
    PHP:
    My guess is there are errors in the queries you're attempting to execute. Throwing that conditional in should pick them up, and by outputting the SQL query you should be able to see what's wrong.
     
    Alex Roxon, Jan 1, 2012 IP
  3. farouqxii

    farouqxii Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think that I got the problem.
    This is where I inserted the code:
    
    if ( $referrer_id == 0 )
    {
        $sql7 = mysql_query( "insert into user_earnings set user_id = '{$user_id}', amount = '{$credit_amount}', description = '{$user_string}', earn_date = '{$now}', earn_type ='direct'" );
        $result = mysql_query($sql) or die($sql . "\n" . mysql_error());
    }
    
    PHP:
    The output is "Query was empty"

    What should I do now?

    Thanks for helping btw! :)
     
    farouqxii, Jan 3, 2012 IP
  4. Bofu2U

    Bofu2U Peon

    Messages:
    105
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You don't need the result line. The mysql_query on the $sql7 is actually executing the query. If you want the insert ID, just do this under it:

    $temp = mysql_insert_id();
     
    Bofu2U, Jan 3, 2012 IP
  5. farouqxii

    farouqxii Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you for the quick respond, but what will this code do?
     
    farouqxii, Jan 3, 2012 IP
  6. Bofu2U

    Bofu2U Peon

    Messages:
    105
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Submit your query, and then assign the inserted ID (if you set a primary key & auto inc "id" field) to the variable $temp
     
    Bofu2U, Jan 3, 2012 IP
  7. farouqxii

    farouqxii Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I don't really understand, the problem is somewhere here:
    
    if ( $referrer_id == 0 )
    {
        $sql7 = mysql_query( "insert into user_earnings set user_id = '{$user_id}', amount = '{$credit_amount}', description = '{$user_string}', earn_date = '{$now}', earn_type ='direct'" );
    }
    else
    {
        $referral_credit = round( $credit_amount * $referral_percentage, 2 );
        $user_credit = round( $credit_amount * ( 1 - $referral_percentage ), 2 );
        $sql9 = mysql_query( "insert into user_earnings set user_id = '{$user_id}', amount = '{$user_credit}', description = '{$user_string}', earn_date = '{$now}', earn_type ='direct'" );
        $sql10 = mysql_query( "insert into user_earnings set user_id = '{$referrer_id}', amount = '{$referral_credit}', description = '{$referal_string}', earn_date = '{$now}', earn_type ='referral'" );
    }
    
    PHP:
    Because this is not getting excuted for a reason, I don't really know why
     
    farouqxii, Jan 3, 2012 IP
  8. AlexanderZ

    AlexanderZ Member

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    48
    #8
    I may be unaware of some new syntax in mysql, but it looks like your sql is invalid. The accepted/correct syntax for insert is:
    
    INSERT INTO `table` (column1, column2, column3) VALUES ('value1', `value2', `value3');
    
    Code (markup):
     
    AlexanderZ, Jan 3, 2012 IP
  9. farouqxii

    farouqxii Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Then how come the last part of the script is getting excuted?
     
    farouqxii, Jan 3, 2012 IP
  10. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #10
    Change to:

    
    if ( $referrer_id == 0 )
    {
        $sql7 = "insert into user_earnings set user_id = '{$user_id}', amount = '{$credit_amount}', description = '{$user_string}', earn_date = '{$now}', earn_type ='direct'";
        $result = mysql_query($sql7) or die($sql7 . "\n" . mysql_error());
    }
    
    PHP:
    Let me know the result.

     
    Alex Roxon, Jan 3, 2012 IP