Jump to content

[SOLVED] troubling script


sandrob57

Recommended Posts

		
$target_id = 3;
$result_war = dbquery("SELECT *	FROM fusion_wars WHERE a_id='".$user_id."' AND d_id='".$target_id."'");

		if (dbrows($result_war) > 0 && dbrows($result_war) < 2){

				$data = dbarray($result_war);

}

echo "$data['a_turns']";

 

$data['a_turns'] is displaying data from where target_id = 2, not 3. It basically isnt pulling data correctly. Why? Ask if you need more info.

Link to comment
Share on other sites

I would try running that query manually.  Or at least printing out the query before executing it.  In this type of situation, it's usually the wrong query, or the data in the database is not what you thought it was.

 

The other common situation is using the wrong result variable, but that looks ok for your script.  I would check that you are actually getting rows back though.. have an "if (dbrows($result_war) == 0) { print "No rows returned??<br>"; }"

Link to comment
Share on other sites

I would try running that query manually.  Or at least printing out the query before executing it.  In this type of situation, it's usually the wrong query, or the data in the database is not what you thought it was.

 

The other common situation is using the wrong result variable, but that looks ok for your script.  I would check that you are actually getting rows back though.. have an "if (dbrows($result_war) == 0) { print "No rows returned??<br>"; }"

It returns the rows. Sadly. This has been driving me crazy for 5 hours. Something in this script is just giving me a hard time.

Link to comment
Share on other sites

Does it return the rows with the data you are expecting?  You can always track down a bug by tracking the data backwards until you find the point at which it changes from being correct to incorrect.  That point is exactly where your bug is.

 

Right now you know that $data['a_turns'] is incorrect.  So go back through your code printing out values until you find where it becomes correct.  Then you have the exact location of your bug :)

Link to comment
Share on other sites

ok, there are two rows in the database table.

 

Once is my username = 1 and one is target user name = 2. The other is 1 and 3, repectively.

 

$result_war = dbquery("SELECT *	FROM fusion_wars WHERE a_id='".$user_id."' AND d_id='".$target_id."'");

		if (dbrows($result_war) > 0 && dbrows($result_war) < 2){

				$data = dbarray($result_war);

}

I have $target_id defined in the url as 2. Why is pulling the data for 3?

Link to comment
Share on other sites

Database Table: fusion_wars

 

Fields: war_id a_id d_id war_start a_turns d_turns a_peace d_peace a_update d_update

 

There are the (only) two rows:

 

Row 1: 79, 1, 2, var, 4, 4, 0, 0, var, var

Row 2: 78, 1, 3, var, 8, 1, 0, 0, var, var

 

(var is just a datestamp)

 

edit: var_dump($data); coming soon

 

edit2: here it is

 

array(10) { ["war_id"]=>  string(2) "78" ["a_id"]=>  string(1) "1" ["d_id"]=>  string(1) "3" ["war_start"]=>  string(10) "1172512146" ["a_turns"]=>  string(1) "8" ["d_turns"]=>  string(1) "1" ["a_peace"]=>  string(1) "0" ["d_peace"]=>  string(1) "0" ["a_update"]=>  string(10) "1172710572" ["d_update"]=>  string(10) "1172710562" } 8

Link to comment
Share on other sites

It appears the data from the database is correct.  It is for target_id 3.  Why do you think that it is incorrect?

 

Edit:  If you look in the var_dump() output, you will see that d_id is set to 3, and the other data is all for the matching row from the db.

Link to comment
Share on other sites

And what is this doing just before your query?

 

$target_id = 3;

No that isn't there on the actual page. This is the entire actualy page.

 

display.php?view=ground&wartype=offensive&target_id=2

 

<?php

if ($page_id == 21){

	if ($view == 'ground' || $view == 'air' || $view == 'missile'){

		include "".BASEDIR."wars/combat/".$view.".php";

	}else{

		redirect("".BASEDIR."news.php");

	}

}else{

	redirect("".BASEDIR."news.php");

}


?>

 

Which calls:

 

<?php

//Base variables

$user_id = $userdata['user_id'];
$target_id = $target_id;
$wartype = $wartype;

if ($wartype != '' && $user_id > 0 && $target_id > 0){

//Do the math
if ($wartype == 'offensive'){ // offense offense offense

	$check = 'true'; 

	$result_war = dbquery("SELECT * FROM `fusion_wars` WHERE `a_id`='".$user_id."' AND `d_id`=".$target_id."");

		if (dbrows($result_war) > 0 && dbrows($result_war) < 2){

				$data = dbarray($result_war);

				echo "".$data['a_turns']."";

				$your_turns = $data['a_turns'];

				$result_1 = dbquery("SELECT * FROM fusion_users WHERE user_id='".$data['d_id']."'");
				if (dbrows($result_1)) { $target = dbarray($result_1); } else { redirect("index.php"); }

		} else {

				$check = 'false';

		}

		if ($check != 'true') {

			redirect("".BASEDIR."index.php");

		}

}elseif ($wartype == 'defensive'){ // defense defense defense

	//coming soon

}else{

	redirect("".BASEDIR."news.php"); //escape

}

}else{

redirect("".BASEDIR."news.php"); //escape

}

//Checks, Checks, and Checks

if($userdata['c_war'] == 'no'  || $target['c_war'] == 'no'){

$check = 'false';

}

if($userdata['user_id'] == $target['user_id']){

$check = 'false';

}

?>

Link to comment
Share on other sites

Ok, here's some steps which will fix your problem.

 

First, echo out the query before running it, like this:

 

$sql = "SELECT * FROM `fusion_wars` WHERE `a_id`='".$user_id."' AND `d_id`=".$target_id." LIMIT 0 , 1";
print "About to run $sql<br>";
$result_war = dbquery($sql);

 

Then, after you've fetched the data, print it out:

 

$data = dbarray($result_war);
print "<pre>"; var_dump($data); print "</pre>";

 

Do they match up?

Link to comment
Share on other sites

Ok, here's some steps which will fix your problem.

 

First, echo out the query before running it, like this:

 

$sql = "SELECT * FROM `fusion_wars` WHERE `a_id`='".$user_id."' AND `d_id`=".$target_id." LIMIT 0 , 1";
print "About to run $sql<br>";
$result_war = dbquery($sql);

 

Then, after you've fetched the data, print it out:

 

$data = dbarray($result_war);
print "<pre>"; var_dump($data); print "</pre>";

 

Do they match up?

I found the problem..

 

On the url, $target_id is set to 2. before I even do the sql lookup, i echoed out $target_id - that stubborn bastard is set at 3.

 

I'm gonna look if anything could have effected, but so far there is literaly no code that could.

Link to comment
Share on other sites

It'll be there somewhere :)

 

A simple method to find where is to print $target_id out at various stages throughout the script.  Somewhere it will change from 2 to 3.  Or it may even be 3 from the very start.

Link to comment
Share on other sites

Ok what the hell. This is the url.

 

viewpage.php?page_id=21&view=ground&wartype=offensive&target_id=2

 

See target ID? Well for some reason, even after I remove ALL THE CODE BEFORE IT it still sets target_id to 3. Are there some ground rules im breaking with calling in url variables? Can you use them in with an included script?

Link to comment
Share on other sites

It'll be there somewhere :)

 

A simple method to find where is to print $target_id out at various stages throughout the script.  Somewhere it will change from 2 to 3.  Or it may even be 3 from the very start.

 

I put echo "2) $target_id"; on every other line. It's 3 the entire time except in the URL.

 

 

EDIT: sure enough the problem lies in my subheader.php file, which uses a target_id variable........I feel so smart for wasting 5 hours on this.

Link to comment
Share on other sites

K now when I do this:

 

$result_war = "SELECT *

FROM fusion_wars

WHERE 'a_id'='$user_id'

AND 'd_id'='$target_id'";

 

$data = dbarray($result_war);

 

var_dump($data);

 

 

I get this for the dump:

 

bool(false)

 

I am 100% sure that target_id is 2 and user_id is 1

Link to comment
Share on other sites

You could be running into some cache(ing) problems on your local pc.

One thing I do is to unset($var) before I reset it. Let's say someone tried a failed login, I have had problems with it just reposting the same data even though it is supposed to post new data. Web browsers do weird things depending on how each user has them set.

Link to comment
Share on other sites

Shouldn't that be `a_id`, not 'a_id' ?

 

Edit: You should edit dbarray() so it checks for errors.  Otherwise you will have more problems like this in future.

 

Edit2:  Where's the call to mysql_query()?  What I meant above is that you should check for errors from mysql_query()

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.