...

View Full Version : PHP Freaky Double Twist



UberDragon
11-15-2006, 01:49 PM
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:

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:


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:o If you are interested in seeing the entire page let me know.

~UberDragon

vinyl-junkie
11-15-2006, 04:59 PM
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.

UberDragon
11-15-2006, 06:30 PM
I have actually.. Here is a larger block of code:

<?php
/* Process previous answers */

$result = $db->query("SELECT user_id FROM users WHERE username = '$user->username'");
$row = mysql_fetch_array($result, MYSQL_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($getid, MYSQL_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($getscore, MYSQL_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....

kaisellgren
11-15-2006, 08:53 PM
I have no idea, but try to replace this
$result = mysql_query($sql_query, $this->link_id);with this
$result = mysql_query($sql_query);

UberDragon
11-15-2006, 09:06 PM
I have no idea, but try to replace this
$result = mysql_query($sql_query, $this->link_id);with this
$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

UberDragon
11-16-2006, 02:17 PM
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:



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

UberDragon
11-16-2006, 04:45 PM
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:



<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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum