Unstoppable Domains

Strange Php error - Been bugging me!!

Spaceship Spaceship
Watch

liam_d

The original NP Emo KidEstablished Member
Impact
25
Ok this bug is really getting on my coding tits!!

Basically this is checking if a forum topic is locked or not.

I have this:
PHP:
// checked the topic is not locked
$locked = mysql_fetch_array(mysql_query("SELECT `locked` FROM `forum_topics` WHERE `id` = '" . quote_smart($_GET['t']) . "'"));

echo "blah" . $locked['locked'];

if ($locked['locked'] == '1')
{
	message($lang['title']['topic_locked'],$lang['text']['locked']);
}

And if anyone signs up at www.milztech.com/forum/ create a topic in the test area and then go to edit it. You will see blah0 meaning the locked string is actually at 0 yet it still says its locked?!!
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
.US domains.US domains
What are you actually trying to do,

Also try:
PHP:
 // checked the topic is not locked 
$locked = mysql_fetch_array(mysql_query("SELECT `locked` FROM `forum_topics` WHERE `id` = \" . quote_smart($_GET['t']) .\"")); 

echo "blah" . $locked['locked']; 

if ($locked['locked'] == '1') 
{ 
    message($lang['title']['topic_locked'].$lang['text']['locked']); 
}
 
0
•••
All i am trying to do is check a variable, yet it always says it's set to 1 when it's not.

What you put won't help no offense.
 
0
•••
fine, at least i tried did you try it then?
 
0
•••
Well for one there should be a , not a . in the message function as a . links them together while , seperates them as there are two parts to the function.

And second quoting what i already have and putting in slashes just does exactly the same thing as what i have in the mysql_query.
 
0
•••
In reality, you shouldn't be using any form of quotes in the SQL around your ID value (not single quotes or double quotes): unless your ID actually isn't a number!

I'm guessing that it's more a problem with the query or the population of the array.

What happens if you add the lines:
print '<pre>';
print_r($locked);
print '</pre>';

after the line where you assign the SQL output to the $locked variable?

Just a guess, but not knowing anything else about your code it's as good as any... I would guess that the locked variable shows to be an empty array, which would mean there's something wrong with one of the MySQL function calls.

If that is the case, call mysql_error() or whatever the function is and see what it says...
 
0
•••
TwistMyArm said:
In reality, you shouldn't be using any form of quotes in the SQL around your ID value (not single quotes or double quotes): unless your ID actually isn't a number!

I'm guessing that it's more a problem with the query or the population of the array.

What happens if you add the lines:
print '<pre>';
print_r($locked);
print '</pre>';

after the line where you assign the SQL output to the $locked variable?

Just a guess, but not knowing anything else about your code it's as good as any... I would guess that the locked variable shows to be an empty array, which would mean there's something wrong with one of the MySQL function calls.

If that is the case, call mysql_error() or whatever the function is and see what it says...

Thanks but read my code and you see this bit:
PHP:
echo "blah" . $locked['locked'];
It comes out as blah0 (i added blah to see where it is incase nothing came out) so it is deffinatly set!
 
0
•••
By 'set' I mean defined: either $locked['locked'] is set to 0, or it's not set at all and so the printout defaults to zero.

I'm guessing the latter, but hey, if you're so sure about what you're doing that you're not even able to try three lines of code, go ahead...
 
0
•••
I didn't mean it like that buddy, i will try what you put, thanks for your help :)

I get this:
PHP:
Array
(
    [0] => 0
    [locked] => 0
)

So it is correct at 0! Yet it doesn't work!
 
Last edited:
0
•••
Can you post more of the code, because it looks like the code you posted is fine.
 
0
•••
Hmmm...

MySQL can be picky, but only sometimes :) What happens if you change your query from:
"SELECT `locked` FROM `forum_topics` WHERE `id` = '" . quote_smart($_GET['t']) . "'"

to:
"SELECT `locked` FROM `forum_topics` WHERE `id` = " . quote_smart($_GET['t']);

(notice the removal of quotes around the ID).

I'd also add:
print mysql_error();

after the query to make sure that it worked, plus... run a print_r on $_GET: just make sure that it was populated correctly, too.

Other than that, yeah, we need more code :)
 
0
•••
I tried all of that, no difference, all the $_GET bits are there. There is no mysql error.

This is really getting to me now :(
 
0
•••
Hmmm... I guess the only other thing I can suggest is that *if* there's a problem with the query, the mysql_error function won't show anything as it only shows the result of the last MySQL function (which is your mysql_fetch_array call).

Have you tried spliting:
Code:
$locked = mysql_fetch_array(mysql_query("SELECT `locked` FROM `forum_topics` WHERE `id` = \" . quote_smart($_GET['t']) .\""));

into two separate calls and check for an error between the two, like...
Code:
$temp = mysql_query("SELECT `locked` FROM `forum_topics` WHERE `id` = \" . quote_smart($_GET['t']) .\"");
print 'First function: ' . mysql_error() . '<br>';
$locked = mysql_fetch_array($temp);
print 'Second function: ' . mysql_error() . '<br>';
 
1
•••
There are no errors being printed out :(
 
0
•••
Oops. Sorry. Just noticed that I copied your original query which probably wouldn't work anyway!

Change:
Code:
$temp = mysql_query("SELECT `locked` FROM `forum_topics` WHERE `id` = \" . quote_smart($_GET['t']) .\"");

to:
Code:
$temp = mysql_query("SELECT `locked` FROM `forum_topics` WHERE `id` = " . quote_smart($_GET['t']) );

while still keeping the error checks in there and then see what happens... Fingers crossed :)

I'd probably also add something like:
Code:
$query = "SELECT `locked` FROM `forum_topics` WHERE `id` = " . quote_smart($_GET['t']);
print 'Query is: ' . $query . '<br>';

Just in case the quote_smart function is doing something funky...
 
0
•••
I now get
Code:
First function:
Second function:
Query is: SELECT `locked` FROM `forum_topics` WHERE `id` = 60
blah0
Array ( [act] => editpost [t] => 60 [f] => 10 [ist] => 1 )
And it still gives me the topic locked box :(

This is the whole page if it helps at all
PHP:
<?php
/*
CherryBB Copyright 2006 Liam Dawe
*/

// Check the group
check_group();

// permissions
$permissions = mysql_fetch_array(mysql_query("SELECT `cpost`,`cview` FROM `permissions` WHERE `group` = '" . quote_smart($_SESSION['group']) . "' AND `forum` = '" . quote_smart($_GET['f']) . "'"));

if ($permissions['cpost'] == '0' || $permissions['cview'] == '0')
{
	message($lang['title']['no_post'],$lang['text']['no_post']);
}

 // checked the topic is not locked
$temp = mysql_query("SELECT `locked` FROM `forum_topics` WHERE `id` = " . quote_smart($_GET['t']) );
print 'First function: ' . mysql_error() . '<br>';
$locked = mysql_fetch_array($temp);
print 'Second function: ' . mysql_error() . '<br>';
$query = "SELECT `locked` FROM `forum_topics` WHERE `id` = " . quote_smart($_GET['t']);
print 'Query is: ' . $query . '<br>';

echo "blah" . $locked['locked'] . "<br />";
print_r ($_GET);

if ($locked['locked'] == '1')
{
    message($lang['title']['topic_locked'],$lang['text']['locked']);
}  

else if (!isset($_GET['action']))
{	
	// Get the forum and category name from the url
	$forum_name = mysql_fetch_array(mysql_query("SELECT `name`,`category` FROM `forums` WHERE `id` = '" . quote_smart($_GET['f']) . "'")) or die(mysql_error());

	$category_name = mysql_fetch_array(mysql_query("SELECT `name` FROM `forums` WHERE `id` = '" . quote_smart($forum_name['category']) . "'"));

	display_clickable_smilies_bbcode();

	// if is topic
	if ($_GET['ist'] == '1')
	{
		$edit_body = mysql_fetch_array(mysql_query("SELECT `body` FROM `forum_topic_text` WHERE `topic_id` = '" . quote_smart($_GET['t']) . "'"));
	}
	

	// if is reply
	else if ($_GET['ist'] == '0')
	{
		$edit_body = mysql_fetch_array(mysql_query("SELECT `body` FROM `forum_reply_text` WHERE `reply_id` = '" . quote_smart($_GET['reply']) . "'"));
	}

	// make post readable for editing
	message($lang['title']['topic_locked'],$lang['text']['locked']);
	
	$edit_body['body'] = edit_post_parser($edit_body['body']);

	// get the file to use
	get_template('editpost');	

}



// Edit Post

else if ($_GET['action'] == 'edit') 

{

	$locker_exists = mysql_fetch_assoc(mysql_query("SELECT * FROM `forum_topics` WHERE `id` = '" . quote_smart($_GET['t']) . "'"));

	

	if ($locker_exists['locked'] == '1') 

	{

		message($lang['title']['topic_locked'],$lang['text']['locked']);

	}

	

	else if (!isset($_GET['t']))

	{

		message($lang['title']['no_topic_id'],$lang['title']['no_topic_id']);

	}

	

	else if (empty($locker_exists)) 

	{

		message($lang['title']['no_topic_id'],$lang['title']['no_topic_id']);

	}

	

	else if (!isset($_POST['body1'])) 

	{

		message($lang['title']['no_body'],$lang['text']['no_body']);

	}

	

	else

	{

		if ($_GET['ist'] == '1')

		{
			// post parsing
			main_post_parser();

			// Insert the reply body
			mysql_query("UPDATE `forum_topic_text` set `body` = '" . quote_smart($_POST['body1']) . "' WHERE `topic_id` = '" . quote_smart($_GET['t']) . "'") or die(mysql_error());

			message($lang['title']['post_edited'], $lang['text']['post_edited']);
		}

		

		else if ($_GET['ist'] == '0')
		{
			// post parsing
			main_post_parser();

			// Insert the reply body
			mysql_query("UPDATE `forum_reply_text` set `body` = '" . quote_smart($_POST['body1']) . "' WHERE `reply_id` = '" . quote_smart($_GET['reply']) . "'") or die(mysql_error());

			message($lang['title']['post_edited'],$lang['text']['post_edited']);

		}

	}

}

?>
 
0
•••
OK... so to be honest, I'm running out of ideas...

What is the output if you add:
print_r ($locked);

after:
print_r ($_GET);

?
 
0
•••
Hello Liam,

The first glimpse at the full code that you have posted above shows me that the problem is in the later part of the code. I will just point out the thing I have noticed.

$_GET['action'] does not exist. Instead it should be $_GET['act']. $_GET fetches a variable from the URL and if you look at the URL, the action variable is called "act" ex. index.php?act=viewtopic. The output from print_r($_GET); also gave you 'act' as the variable and not 'action'.

Try changing all the occurrences of $_GET['action'] to $_GET['act'].

I will take a good look at the code maybe tomorrow during my free time.
 
0
•••
Thanks for your reply but act and action are actually different things.

Act is the module which is being loaded while action is something within the script that is going on.

I will add the other print_r when i get back to my desk later on today.

Thanks.
 
0
•••
TwistMyArm said:
OK... so to be honest, I'm running out of ideas...

What is the output if you add:
print_r ($locked);

after:
print_r ($_GET);

?

i now have this, sorry for such a late reply
Code:
First function:
Second function:
Query is: SELECT `locked` FROM `forum_topics` WHERE `id` = 3
blah0
Array ( [act] => editpost [t] => 3 [f] => 10 [ist] => 1 )
Array ( [0] => 0 [locked] => 0 )
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Appraise.net
Domain Recover
DomainEasy โ€” Live Options
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back