Help a noob with $_GET[id]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Xager
Forum Newbie
Posts: 10
Joined: Thu Aug 23, 2007 2:18 am
Location: Australia

Help a noob with $_GET[id]

Post by Xager »

I have this script working somewhere else but I got it from a tutorial and so don't exactly understand what I am doing.

Code: Select all

<?php
	include ('mysql_connect.php');
	if (isset($_GET['id'])) {
	$id = $_GET['newstype'];
	} else {
	echo 'Please select a news type.';
	exit();
	}
	$query = "SELECT * FROM news_posts WHERE newstype=$id";
	$result = @mysql_query($query);
	if ($result) {
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
	echo "<table cellspacing=0 cellpadding=0 border=0>
		<tr>
			<td>$row[title]</td>
		</tr>
	</table>"; }
	}
?>
I have this link coming from my homepage which is mainly news. i have 5 different newstypes the one that I am mainly focusing on is cc. so i have the link:

Code: Select all

<a href=posts.php?id=$row[newstype]><img src=images/$row[newstype].gif border=0 /></a>
to go to my posts page (code up above), now I dont know why it isnt working properly, anyone able to shed some light?

Cheers,
Xager
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Whats not working?

Put error_reporting(E_ALL); at the top of your pages, when you are debugging.

Try printing out $_GET to see if its getting the data you want it to get, ie: an integer.

Code: Select all

print '<pre>'; print_r ($_GET); print  '</pre>';
Xager
Forum Newbie
Posts: 10
Joined: Thu Aug 23, 2007 2:18 am
Location: Australia

Post by Xager »

with the code i am using i get a blank page.

i get this

Array
(
[id] => cc
)

which i think is what i want. I want the page to display the information that is associated with the newstype "cc"
Last edited by Xager on Fri Aug 24, 2007 10:49 am, edited 1 time in total.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Code: Select all

$query = "SELECT * FROM `news_posts` WHERE newstype='$id'"; // Notice the Single Quotes Since $id is NOT a number 
Xager
Forum Newbie
Posts: 10
Joined: Thu Aug 23, 2007 2:18 am
Location: Australia

Post by Xager »

It still doens't work. And still displays the same error_reporting message.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Whats the message?

And right now your code is checking to see it $_GET['id'] is set, and then you set $id as $_GET['newstype']

Well From what you posted, $_GET['newstype'] does not exist, maybe you want..

Code: Select all

$id = $_GET['id']; 
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post by Citizen »

Make sure you're getting the integer value of "id" when you do and if($_GET[id]) since a value of 0 would come up false.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Citizen wrote:Make sure you're getting the integer value of "id" when you do and if($_GET[id]) since a value of 0 would come up false.
Hes using isset, so thats Irreverent.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

Code: Select all

<a href=posts.php?id=$row[newstype]><img src=images/$row[newstype].gif border=0 /></a>
Try assigning the value to a variable before using it in an expression:

Code: Select all

$newstype=$row[newstype];
$gif=$row[newstype]."gif";
...
<a href=posts.php?id=$newstype><img src=images/$gif border=0 /></a>
Xager
Forum Newbie
Posts: 10
Joined: Thu Aug 23, 2007 2:18 am
Location: Australia

Post by Xager »

I am getting the right information to the posts.php page. I am trying to select all of my "news posts" with the newstype of, in this case, "cc". My website is http://www.xager.net, if you click on each of the newstype images, "cc" meaning Copper's Chronicles, I want it to load up the posts.php page and show all of the Copper's Chronicles posts. I have a similar setup going for my news post comment system. Clicking on the link to View / Add Comments takes the viewer to the comments.php page, shows the post they are viewing / commenting on and then shows any comments aswell as the comment submission form.

I dont know that much PHP so I could be doing something totally wrong. Once thing I am thinking I could be doing wrong is: At the moment there are 2 news posts with the "newstype" of "cc", could I be trying to show both of them at the same time but they cancel eachother out or something like that?

Thanks for your help everyone, really appreciate it.

Regards,
Xager
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

If you have more than one result, mysql_fetch_array() will take the last result, so that is not a problem (although probably not what you want, you want to use a while() loop).

When printing your variables from an array, you should do it like this:

Code: Select all

echo "this is an array value " . $array['key'] . " here";

//or

echo "this is an array value {$array['key']} here";
Not sure if that is your problem. Are you getting any errors? Stick the following code at the top of your page.

Code: Select all

ini_set('display_errors', 'On');
error_reporting(E_ALL);
Also remove the @ from in front of $result. The use of mysql_error() during the query might help.

Code: Select all

$result = mysql_query($query) or die(mysql_error())
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Xager
Forum Newbie
Posts: 10
Joined: Thu Aug 23, 2007 2:18 am
Location: Australia

Post by Xager »

I have changed

Code: Select all

$row[$title]
to

Code: Select all

$array['title']
and removed the @ and inserted the

Code: Select all

ini_set('display_errors', 'On');
error_reporting(E_ALL);
and there is no change, apart from the fact that i now the the MySQL error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''news_posts' WHERE newstype='cc'' at line 1

MySQL server version is: 4.1.22-standard; and i am obviously getting the correct newstype.

So, any ideas?

UPDATE: I have removed some of the ' and " from the query and am now getting:
Unknown column 'cc' in 'where clause'

Here is my full code for posts.php:

Code: Select all

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
	include ('mysql_connect.php');
	if (isset($_GET['id'])) {
	$id = $_GET['id'];
	} else {
	echo 'Please select a news type.';
	exit();
	}
	$query = "SELECT * FROM news_posts WHERE newstype=$id";
	$result = mysql_query($query) or die(mysql_error());
	if ($result) {
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
	echo "this is an array value " . $array['title'] . " here"; }
	}	
?>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Yes, it is looking for column `cc` which is not a number, so needs to go in single quotes. = '$id' in your $query.

Also, $array['title'] should be $row['title'] in your code, since you named your array $row. ;)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Xager
Forum Newbie
Posts: 10
Joined: Thu Aug 23, 2007 2:18 am
Location: Australia

Post by Xager »

Yay. Thanks so much for your help. Working find now. I have only been doing PHP for less than a week so I am still learning heaps. Cheers for all the input.

Regards,
Xager
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Sigh.., my second post pointed out that $id was not a number so it should be quoted.
Zoxive wrote:

Code: Select all

$query = "SELECT * FROM `news_posts` WHERE newstype='$id'"; // Notice the Single Quotes Since $id is NOT a number
Post Reply