Page 1 of 2
Help a noob with $_GET[id]
Posted: Fri Aug 24, 2007 10:25 am
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
Posted: Fri Aug 24, 2007 10:40 am
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>';
Posted: Fri Aug 24, 2007 10:46 am
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"
Posted: Fri Aug 24, 2007 10:49 am
by Zoxive
Code: Select all
$query = "SELECT * FROM `news_posts` WHERE newstype='$id'"; // Notice the Single Quotes Since $id is NOT a number
Posted: Fri Aug 24, 2007 10:54 am
by Xager
It still doens't work. And still displays the same error_reporting message.
Posted: Fri Aug 24, 2007 1:12 pm
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..
Posted: Fri Aug 24, 2007 2:21 pm
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.
Posted: Fri Aug 24, 2007 2:43 pm
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.
Posted: Fri Aug 24, 2007 4:47 pm
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>
Posted: Fri Aug 24, 2007 9:42 pm
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
Posted: Fri Aug 24, 2007 9:57 pm
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())
Posted: Sat Aug 25, 2007 1:53 am
by Xager
I have changed
to
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"; }
}
?>
Posted: Sat Aug 25, 2007 2:46 am
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.

Posted: Sat Aug 25, 2007 5:12 am
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
Posted: Sun Aug 26, 2007 11:53 am
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