Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question PHP Freaky Double Twist

    Ok.. this has got to be the strangest PHP experience I've ever had... I should preface this project started out an open source program (that is no longer available) that was broken (they brutalized the code then given to me to perform a specific purpose).. I had to rewrite virtually the entire script... With that said let me explain the current issue keeping my head shiney bald:

    A single line of code is writing to the database twice when it should only be happening once. I have been writing PHP code for over a year now and I just can't figure it out. When I comment out the single line of code, nothing is written to the database. Yes I'm using a built in database connection class of some sort that was apart of the original program, but I've analysed it and it shouldn't be activating twice either (and doesn't when used elsewhere in the program code).

    Here is my if statement:
    PHP Code:
    if ($last == 0) { 
        echo 
    'LAST is '.$last.' biatch'$user_id;
        
    $today date("Y-m-d H:i:s");
        
    $db->query("INSERT INTO results (test_id,user_id,points,points_possible,date) VALUES ('43','$user_id','0','12','$today')");

    Please assume $last does in fact == 0 and $user_id is a valid number. Below is the function within the database connection class that handles the query:

    PHP Code:
        function query($sql_query)
        {
            if (
    $this->debug) {
                
    fwrite($this->fp$this->format_date() . " Query: $sql_query\n");
            }

            
    $result mysql_query($sql_query$this->link_id);

            if (!
    $result) {
                
    $this->sql_error($sql_query);
            }
            return 
    $result;
        } 
    Any ideas? There is no loop that would execute the if statement twice. Commenting stops all execution... but a single page hit generates two rows in the database... Help If you are interested in seeing the entire page let me know.

    ~UberDragon
    Last edited by UberDragon; 11-15-2006 at 02:16 PM.

  • #2
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,073
    Thanks
    2
    Thanked 22 Times in 22 Posts
    Have you tried putting echo statements in various places within the code to try and see what's happening when that code is being executed twice? That's what I do when I can't see what's happening merely by looking at the code.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!

  • #3
    New Coder
    Join Date
    Nov 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have actually.. Here is a larger block of code:
    PHP Code:
    <?php
    /* Process previous answers */

        
    $result $db->query("SELECT user_id FROM users WHERE username = '$user->username'");
        
    $row mysql_fetch_array($resultMYSQL_ASSOC);
        
    $user_id $row[user_id];

    if (
    $last == 0) { 
        echo 
    'LAST is '.$last.' User '$user_id;
        
    $today date("Y-m-d H:i:s");
        
    $db->query("INSERT INTO results (test_id,user_id,points,points_possible,date) VALUES ('43','$user_id','0','12','$today')");
    }

        
    $getid $db->query("SELECT result_id FROM results WHERE user_id = '$user_id'");
        
    $row mysql_fetch_array($getidMYSQL_ASSOC);
        
    $result_id $row[result_id];
    echo 
    '<h1>HERE '.$result_id;
        
    $getscore $db->query("SELECT points FROM results WHERE result_id = '$result_id'");
        
    $row2 mysql_fetch_array($getscoreMYSQL_ASSOC);
        
    $old_points $row2[points];
    echo 
    '<h1>HERE '.$old_points;
    echo 
    '<h1>HERE '.$result_id;

    if (
    $last >= 1) {

    echo 
    'Last is '.$last;
    echo 
    '<h1>HERE '.$result_id;


    $db->query("INSERT INTO answers (result_id,question_number,question,answer,users_answer) VALUES ('$result_id','$last','$question','$correctchoice','$answer')");

        if (
    $answer == $correctchoice) { 
            
    $points $old_points 1; echo "<h1>old points:$old_points and now points:$points";
            
    $db2->query("UPDATE results SET points = '$points' WHERE result_id = '$result_id'");
        }
        else { 
    $points $old_points; }

        
    $score round( ( ( $points 12 ) * 100 ) ) ;
        
    $db2->query("UPDATE results SET score = '$score' WHERE result_id = '$result_id'");
    }

    if (
    $last==12) {
        
    pt_redirect('evaluations.php?eval_id=9');
    }


    /*End of Process */
    And my output =

    LAST is 0 User 508
    HERE 615
    HERE 0
    HERE 615

    Also it is very interesting to note... the $points are updated by 2's also.. O.O watch the output

    Question 1 so $last = 0
    Output:

    LAST is 0 User 508
    HERE 628
    HERE 0
    HERE 628

    Database rows effected are:
    result_id test_id user_id points points_possible score date
    629 43 508 0 12 0 2006-11-15 12:25:01
    628 43 508 0 12 0 2006-11-15 12:25:00


    Question 2 so $last = 1
    Output:

    HERE 629
    HERE 0
    HERE 629Last is 1
    HERE 629
    old points:0 and now points:1

    Database rows:
    result_id test_id user_id points points_possible score date
    629 43 508 2 12 17 2006-11-15 12:25:01
    628 43 508 0 12 0 2006-11-15 12:25:00

    Question 3 so $last ==2
    Output:

    HERE 629
    HERE 2
    HERE 629Last is 2
    HERE 629
    old points:2 and now points:3

    Database:
    result_id test_id user_id points points_possible score date
    629 43 508 4 12 33 2006-11-15 12:25:01
    628 43 508 0 12 0 2006-11-15 12:25:00

    All the questions were answered correctly. Since we are on Question 3 it has not been answered yet. Therefore there should be a total of 2 in the points column. Its doubling too o.O but my echo's aren't....

  • #4
    Regular Coder
    Join Date
    Jan 2006
    Location
    Finland, Hollola
    Posts
    285
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I have no idea, but try to replace this
    PHP Code:
    $result mysql_query($sql_query$this->link_id); 
    with this
    PHP Code:
    $result mysql_query($sql_query); 
    PHP 5 & MySQL 5 (Y)

  • #5
    New Coder
    Join Date
    Nov 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by kaisellgren View Post
    I have no idea, but try to replace this
    PHP Code:
    $result mysql_query($sql_query$this->link_id); 
    with this
    PHP Code:
    $result mysql_query($sql_query); 
    Just for giggles I made the change for the class function described here, but results are the same. Still doubling up the query

  • #6
    New Coder
    Join Date
    Nov 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This just gets more and more strange....

    So to eliminate the class db connection I have rewritten the following section bringing it back to the basics:

    PHP Code:
    if ($last == 0) { 
        echo 
    'LAST is '.$last.' User '$user_id;
        
    $today date("Y-m-d H:i:s");
    //    $db->query("INSERT INTO results (test_id,user_id,points,points_possible,date) VALUES ('43','$user_id','0','12','$today')");

        
    $con mysql_connect(DB_HOST,DB_USERNAME,DB_PASSWORD);
        if (!
    $con)  {
            die(
    'Could not connect: ' mysql_error());
          }

        
    mysql_select_db(DEFAULT_DB$con);

        
    mysql_query("INSERT INTO results (test_id,user_id,points,points_possible,date) VALUES ('43','$user_id','0','12','$today')");

    Would you believe this produces double posts as well? I'm beginning to think this is not my script, but a problem with the database itself.. This project is due soon.. I hope you guys have some good suggestions soon lol :P

    ~UberDragon

  • #7
    New Coder
    Join Date
    Nov 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    The Answer!

    Ok after taking apart this script piece by piece I have finally determined what was causing the double posts to the database.. Earlier in the script I include a header file. In that header file was the following:

    PHP Code:
    <img src="" border="0"
    Once I commented out the img tag with the empty src no more double posts.. I know, I know this makes absolutely no sense. But in the end that was the answer.

    See ya next time!

    ~UberDragon


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •