Jump to content

Messed up function?


3raser

Recommended Posts

Whenever a valid ID is entered, my page stops loading and when it finally does load, it gives a lot of MySQL connection errors.

 

What is wrong with my function code?

 

<?php
include_once("includes/config.php");

if(!$_GET['id'] && $_POST['id'])
{
$id = mysql_real_escape_string($_POST['id']);
}
elseif($_GET['id'] && !$_POST['id'])
{
$id = mysql_real_escape_string($_GET['id']);
}
else
{

}

if(!$id)
{
$content = "Sorry, you have not selected a skin to view.";
}
else
{
$extract_information = mysql_query("SELECT title,username,id,password,description FROM skins WHERE id = '$id' LIMIT 1");

function displayBody($id)
{
	mysql_query("UPDATE skins SET views = views + 1 WHERE id = '$id'");

	$content = (displayBody($id)) ? $extract['title']. ", by ". $extract['username'] .".<br/><br/>Description: ". $extract['description'] ." -
	<a href='view.php?download=". $extract['id'] ."'>Download</a><br/><br/>
	<img src='skins/". $extract['id'] .".png' width='500' height='300'>" : NULL;

	return true;
}

if(mysql_num_rows($extract_information) == 0)
{
	$content = "Sorry, no skin exists with this ID.";
}
else
{
	$extract = mysql_fetch_assoc($extract_information);

	if($_GET['download'])
	{
		$does_exist_download = mysql_query("SELECT title,username,id,password,description FROM skins WHERE id = '$id' LIMIT 1");

		if(mysql_num_rows($does_exist_download) == 0)
		{
			$content = "No skin exists with this ID, so you may not download.";
		}
		else
		{
		mysql_query("UPDATE skins SET downloads = downloads + 1 WHERE id = '". mysql_real_escape_string($_GET['download']) ."'");

			header("Pragma: public");
			header("Expires: 0");
			header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 

			header("Content-Type: application/force-download");
			header( "Content-Disposition: attachment; filename=skins/".$extract['id'].".png");

			header( "Content-Description: File Transfer");
			@readfile($file);

		}


	}
	elseif(!$extract['password'])
	{
		displayBody($id);
	}
	elseif(!$_POST['password'])
	{
		$content = "<br/><br/><div id='header'>Password</div> <center><form action='view.php' method='POST'>
		<input type='hidden' name='id' value='". $id ."'>Password: <input type='password' name='password' maxlength='6'>
		<input type='submit' value='View'></form></center>";
	}
	else
	{
		if($_POST['password'] != $extract['password'])
		{
			$content = "You have entered in an incorrect password. <a href='view.php?id=". $id ."'>Try Again</a> or <a href='index.php'>Home</a>.";
		}
		else
		{
			displayBody($id);
		}
	}
}
}

?>
<html>
<head>
<title><?php $title; ?></title>
<link rel="stylesheet" type="text/css" href="theme/style.css" />
</head>
<body>
<div id="header">
MCSkins
</div>

<?php echo $content; ?>
</center>

</body>
</html>

Link to comment
Share on other sites

You might want to tell us what errors your getting then. I don't much like guessing games.

 

If the page would load I'd show you.

 

:/

http://stonedknights.net46.net/view.php?id=14

 

it gives a lot of MySQL connection errors.

 

Ummm, ???

 

Anyway, I told you in another thread that your variables are out of scope in the function displayBody(), I also told you how to fix it.

 

You should really read up on variable scope.

Link to comment
Share on other sites

You might want to tell us what errors your getting then. I don't much like guessing games.

 

If the page would load I'd show you.

 

:/

http://stonedknights.net46.net/view.php?id=14

 

it gives a lot of MySQL connection errors.

 

Ummm, ???

 

Anyway, I told you in another thread that your variables are out of scope in the function displayBody(), I also told you how to fix it.

 

You should really read up on variable scope.

 

Yes, and I'm using the code you suggested for me to use. :/

 

And reading up now.

Link to comment
Share on other sites

Ok, it seems changing my code to this:

 

function displayBody($id)
{
	global $content;

	mysql_query("UPDATE skins SET views = views + 1 WHERE id = '$id'");

	$content = $extract['title']. ", by ". $extract['username'] .".<br/><br/>Description: ". $extract['description'] ." -
	<a href='view.php?download=". $extract['id'] ."'>Download</a><br/><br/>
	<img src='skins/". $extract['id'] .".png' width='500' height='300'>";
}

 

Gave me this...weird error:

 

User 'a3473343_data' has exceeded the 'max_questions' resource (current value: 100000)

Link to comment
Share on other sites

Sorry, I focused on the return part, and left out the other parts.

 

function displayBody($id) {
  $result = mysql_query("UPDATE skins SET views = views + 1 WHERE id = '$id'");
  return true;
}

 

Then:

$content = (displayBody($id)) ? $extract['title']. ", by ". $extract['username'] ."." : NULL;

 

You ran out of scope with the function, you must put ID into the function scope.  As well, the $extract array is out of scope with the function.  Since there is no reason to send an argument to the function just to return it, handle the $extracts outside of the function, based on how the function acts.  So, we return the function as true, to enable us to decide which string should display.

Link to comment
Share on other sites

Justin, don't use globals in functions. They break the entire point of encapsulation.

 

I didn't see the rest of your code in your post (didn't scroll down for whatever reason) but yeah, there is quite allot of issues within. I suggest your take a good look at the link that jcbones provided on scope and maybe start again.

Link to comment
Share on other sites

Justin, don't use globals in functions. They break the entire point of encapsulation.

 

I didn't see the rest of your code in your post (didn't scroll down for whatever reason) but yeah, there is quite allot of issues within. I suggest your take a good look at the link that jcbones provided on scope and maybe start again.

 

Er....global is pretty much based from the link he gave me, and it's really the only thing it talks about scopes. :?

Link to comment
Share on other sites

Functions accept arguments and return values, you use these to pass data in and get data out of functions. Using globals breaks a functions encapsulation and is considered poor design.

 

Is their a documentation on that then?

 

I really don't see why global is that bad though, it just lets other section of the code access the data and also makes changes to $content outside the function, which is exactly what I need it do do.

Link to comment
Share on other sites

Is their a documentation on that then?

 

Within the functions section of the manual.

 

I really don't see why global is that bad though, it just lets other section of the code access the data and also makes changes to $content outside the function, which is exactly what I need it do do.

 

Most newcomers don't. That's why it's such a trap. The bigger picture is that eventually you will be working with someone else's code. Now, lets say they have a function documented like so....

 

void foo ( string $name )

 

This says that the function foo excepts 1 argument, a string to be used within the function as $name. Now, if this function also relies upon a global $content you have no way of knowing about it and you cannot use the function without first defining $content somewhere.

 

Functions accept arguments so that they are self documenting, sure, you could go and look at the code for the foo() function but you shouldn't have to.

Link to comment
Share on other sites

Is their a documentation on that then?

 

Within the functions section of the manual.

 

I really don't see why global is that bad though, it just lets other section of the code access the data and also makes changes to $content outside the function, which is exactly what I need it do do.

 

Most newcomers don't. That's why it's such a trap. The bigger picture is that eventually you will be working with someone else's code. Now, lets say they have a function documented like so....

 

void foo ( string $name )

 

This says that the function foo excepts 1 argument, a string to be used within the function as $name. Now, if this function also relies upon a global $content you have no way of knowing about it and you cannot use the function without first defining $content somewhere.

 

Functions accept arguments so that they are self documenting, sure, you could go and look at the code for the foo() function but you shouldn't have to.

 

So the code I'm looking for is:

 

function displayBody($id)
{
	mysql_query("UPDATE skins SET views = views + 1 WHERE id = '$id'");

	$content = $extract['title']. ", by ". $extract['username'] .".<br/><br/>Description: ". $extract['description'] ." -
	<a href='view.php?download=". $extract['id'] ."'>Download</a><br/><br/>
	<img src='skins/". $extract['id'] .".png' width='500' height='300'>";

	return $content;
}

 

Sorry if I'm getting annoying, I just like to make sure I do things right before I start putting it into action.

 

And right now I don't have the option to test, seeing as my website is under review. I hate my host. -.-

 

EDIT: After this post is solved, can an admin/moderator delete all my posts/threads? My 500 posts are usually just questions, and I feel thats a bit....crappy.

Link to comment
Share on other sites

$extract is not defined within that function.

 

So, would it be included with $id in the parameter?

 

Also, can you reset my post count and delete all my threads and posts besides this one, or just delete everything after this is solved? Have 500 questions as postcount makes me feel like the 500 is....well...not deserved. ;)

Link to comment
Share on other sites

So, would it be included with $id in the parameter?

 

Yes, if you want to use it within your function you will need to pass it into the function.

 

Also, can you reset my post count and delete all my threads and posts besides this one, or just delete everything after this is solved? Have 500 questions as postcount makes me feel like the 500 is....well...not deserved.

 

As you have just noted, post counts are not an indication of knowledge, in fact, there not really an indication of anything except how many times you've hit the submit button. We do not tamper ewith people's counts however.

Link to comment
Share on other sites

Erhm, changed my code too this, and now the page will take 30 seconds to load. It seems the function may be repeating itself over and over and over, because sometimes the views row jumps to 20 thousand.

 

<?php
include_once("includes/config.php");

if(!$_GET['id'] && $_POST['id'])
{
$id = mysql_real_escape_string($_POST['id']);
}
elseif($_GET['id'] && !$_POST['id'])
{
$id = mysql_real_escape_string($_GET['id']);
}
else
{

}

if(!$id)
{
$content = "Sorry, you have not selected a skin to view.";
}
else
{
$extract_information = mysql_query("SELECT title,username,id,password,description FROM skins WHERE id = '$id' LIMIT 1") or die(mysql_error());

function displayBody($id,$extract)
{
	mysql_query("UPDATE skins SET views = views + 1 WHERE id = '$id'");

	$content = $extract['title']. ", by ". $extract['username'] .".<br/><br/>Description: ". $extract['description'] ." -
	<a href='view.php?download=". $extract['id'] ."'>Download</a><br/><br/>
	<img src='skins/". $extract['id'] .".png' width='500' height='300'>";

	return $content;
}

if(mysql_num_rows($extract_information) == 0)
{
	$content = "Sorry, no skin exists with this ID.";
}
else
{
	$extract = mysql_fetch_assoc($extract_information);

	if($_GET['download'])
	{
		$does_exist_download = mysql_query("SELECT title,username,id,password,description FROM skins WHERE id = '$id' LIMIT 1");

		if(mysql_num_rows($does_exist_download) == 0)
		{
			$content = "No skin exists with this ID, so you may not download.";
		}
		else
		{
		mysql_query("UPDATE skins SET downloads = downloads + 1 WHERE id = '". mysql_real_escape_string($_GET['download']) ."'");

			header("Pragma: public");
			header("Expires: 0");
			header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 

			header("Content-Type: application/force-download");
			header( "Content-Disposition: attachment; filename=skins/".$extract['id'].".png");

			header( "Content-Description: File Transfer");
			@readfile($file);

		}


	}
	elseif(!$extract['password'])
	{
		displayBody($id);
	}
	elseif(!$_POST['password'])
	{
		$content = "<br/><br/><div id='header'>Password</div> <center><form action='view.php' method='POST'>
		<input type='hidden' name='id' value='". $id ."'>Password: <input type='password' name='password' maxlength='6'>
		<input type='submit' value='View'></form></center>";
	}
	else
	{
		if($_POST['password'] != $extract['password'])
		{
			$content = "You have entered in an incorrect password. <a href='view.php?id=". $id ."'>Try Again</a> or <a href='index.php'>Home</a>.";
		}
		else
		{
			displayBody($id);
		}
	}
}
}

?>
<html>
<head>
<title><?php $title; ?></title>
<link rel="stylesheet" type="text/css" href="theme/style.css" />
</head>
<body>
<div id="header">
MCSkins
</div>

<?php echo $content; ?>
</center>

</body>
</html>

Link to comment
Share on other sites

Code:

 

<?php
include_once("includes/config.php");

if(!$_GET['id'] && $_POST['id'])
{
$id = mysql_real_escape_string($_POST['id']);
}
elseif($_GET['id'] && !$_POST['id'])
{
$id = mysql_real_escape_string($_GET['id']);
}
else
{

}

if(!$id)
{
$content = "Sorry, you have not selected a skin to view.";
}
else
{
$extract_information = mysql_query("SELECT title,username,id,password,description FROM skins WHERE id = '$id' LIMIT 1") or die(mysql_error());

function displayBody($id,$extract)
{
	mysql_query("UPDATE skins SET views = views + 1 WHERE id = '$id'");

	$content = $extract['title']. ", by ". $extract['username'] .".<br/><br/>Description: ". $extract['description'] ." -
	<a href='view.php?download=". $extract['id'] ."'>Download</a><br/><br/>
	<img src='skins/". $extract['id'] .".png' width='500' height='300'>";

	return $content;
}

if(mysql_num_rows($extract_information) == 0)
{
	$content = "Sorry, no skin exists with this ID.";
}
else
{
	$extract = mysql_fetch_assoc($extract_information);

	if($_GET['download'])
	{
		$does_exist_download = mysql_query("SELECT title,username,id,password,description FROM skins WHERE id = '$id' LIMIT 1");

		if(mysql_num_rows($does_exist_download) == 0)
		{
			$content = "No skin exists with this ID, so you may not download.";
		}
		else
		{
		mysql_query("UPDATE skins SET downloads = downloads + 1 WHERE id = '". mysql_real_escape_string($_GET['download']) ."'");

			header("Pragma: public");
			header("Expires: 0");
			header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 

			header("Content-Type: application/force-download");
			header( "Content-Disposition: attachment; filename=skins/".$extract['id'].".png");

			header( "Content-Description: File Transfer");
			@readfile($file);

		}


	}
	elseif(!$extract['password'])
	{
		displayBody($id);
	}
	elseif(!$_POST['password'])
	{
		$content = "<br/><br/><div id='header'>Password</div> <center><form action='view.php' method='POST'>
		<input type='hidden' name='id' value='". $id ."'>Password: <input type='password' name='password' maxlength='6'>
		<input type='submit' value='View'></form></center>";
	}
	else
	{
		if($_POST['password'] != $extract['password'])
		{
			$content = "You have entered in an incorrect password. <a href='view.php?id=". $id ."'>Try Again</a> or <a href='index.php'>Home</a>.";
		}
		else
		{
			displayBody($id);
		}
	}
}
}

?>
<html>
<head>
<title><?php $title; ?></title>
<link rel="stylesheet" type="text/css" href="theme/style.css" />
</head>
<body>
<div id="header">
MCSkins
</div>

<?php echo $content; ?>
</center>

</body>
</html>

 

The function:

 

function displayBody($id,$extract)
{
	mysql_query("UPDATE skins SET views = views + 1 WHERE id = '$id'");

	$content = $extract['title']. ", by ". $extract['username'] .".<br/><br/>Description: ". $extract['description'] ." -
	<a href='view.php?download=". $extract['id'] ."'>Download</a><br/><br/>
	<img src='skins/". $extract['id'] .".png' width='500' height='300'>";

	return $content;
}

 

Error:

 

Warning: Missing argument 2 for displayBody(), called in /home/a3473343/public_html/view.php on line 72 and defined in /home/a3473343/public_html/view.php on line 25

Link to comment
Share on other sites

"Missing argument 2" is shorthand for "You are missing the second argument/parameter"

 

displayBody($id);

 

See, there is only one argument

 

Well, I managed to get rid of the error. But why is it when I view my page, no results are echoed?...

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.