...

View Full Version : newbie needs help with isset $_GET



mastertooley
09-07-2009, 05:17 AM
Hello all,

I'm new with PHP and looking for some help... can someone tell me what am I doing wrong here why can't I make this work, I know I'm doing something wrong,

Thank you in advance..


<?php

//Retrieves data from MySQL

if(isset($_GET['id']) && $id !=''){
$id = mysql_real_escape_string($id);
$sql = "SELECT * FROM bloggers WHERE id = '$id'";

}else{
$sql = "SELECT * FROM bloggers ORDER BY id DESC";

}

$data = mysql_query($sql) or die(mysql_error());
//Puts it into an array
if(mysql_num_rows($data) != 0){
while($info = mysql_fetch_assoc( $data ))
{

//Outputs the image and other data
Echo '<h1><a href=?id=' . $row['id'] . '>' . $row['title'] .$info['title'] . '</a></h1>';
Echo "<h4>".$info['timestamp'] . " </h4>";
Echo "<p> ".$info['body'] . " </p>";

Echo '<img src=http://www.example.com'.$info['image'] .'> <br/><br/><div class="line"></div>';
}
}
?>

funnymoney
09-07-2009, 05:26 AM
Hello all,

I'm new with PHP and looking for some help... can someone tell me what am I doing wrong here why can't I make this work, I know I'm doing something wrong,

Thank you in advance..



<?php

//Retrieves data from MySQL

if(isset($_GET['id']) && $id !=''){
$id = mysql_real_escape_string($id);
$sql = "SELECT * FROM bloggers WHERE id = '$id'";

}else{
$sql = "SELECT * FROM bloggers ORDER BY id DESC";

}

$data = mysql_query($sql) or die(mysql_error());
//Puts it into an array
if(mysql_num_rows($data) != 0){
while($info = mysql_fetch_assoc( $data ))
{

//Outputs the image and other data
Echo '<h1><a href=?id=' . $row['id'] . '>' . $row['title'] .$info['title'] . '</a></h1>';
Echo "<h4>".$info['timestamp'] . " </h4>";
Echo "<p> ".$info['body'] . " </p>";

Echo '<img src=http://www.example.com'.$info['image'] .'> <br/><br/><div class="line"></div>';
}
}
?>

can you please put php code in php bbcode :)
thanks.. now let's see...

funnymoney
09-07-2009, 05:27 AM
oh, here


if(isset($_GET['id']) && $id !=''){
$id = mysql_real_escape_string($id);
$sql = "SELECT * FROM bloggers WHERE id = '$id'";

}else{
$sql = "SELECT * FROM bloggers ORDER BY id DESC";

}

you need to declare $id variable like this

if(isset($_GET['id']) && $id !=''){
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$sql = "SELECT * FROM bloggers WHERE id = '$id'";

}else{
$sql = "SELECT * FROM bloggers ORDER BY id DESC";

}

dunno about other mistakes yet

mastertooley
09-07-2009, 05:40 AM
Hi funnymoney,

Thank you for your quick answer, sorry about the ' php bbcode' but I don't know what you mean with that, tried your answer to define the var but still no luck.

Thank you for your help.

regards

Zangeel
09-07-2009, 05:40 AM
How can you check the variable $id if it's not created yet?

That's like doing this



<?php
if ($variable == 1)
{
$variable = 1;
echo 'This code doesn\'nt make sense :)';
}
?>


Create the variable BEFORE checking it.

funnymoney
09-07-2009, 05:43 AM
i missed this what Zangeel says.. you can't check $id before you set id

so this has no sense


if(isset($_GET['id']) && $id !=''){

because you didn't set $id value yet

this is correct


if(isset($_GET['id'])){

to check weather $id is empty, you need to do that inside isset control, and then output results

Zangeel
09-07-2009, 05:49 AM
And of course you can just add the empty check, using the $_GET variable.


if ( isset ( $_GET['id'] ) && !empty ( $_GET['id'] ) ) { ...

mastertooley
09-07-2009, 05:57 AM
Thank you all for your suggestions and for pointing out my newbie mistakes!! I'm afraid that still no luck I still getting ' index.php?id= ' and I know its a stupid and silly mistake but when one does not have much experience thats what happens.

Thank you guys for your support and help

regards

Zangeel
09-07-2009, 06:09 AM
Here's how I do it.



<?php
if (isset($_GET['id'])) {

$id = (!empty($_GET['id'])) ? mysql_real_escape_string($_GET['id']) : null;
$sql = ($id === null) ? 'SELECT * FROM `bloggers` ORDER BY `id` DESC' :
'SELECT * FROM `bloggers` WHERE `id` = \'' . $id . ' '\'';

$data = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($data) != 0) {
while ($info = mysql_fetch_assoc($data)) {

echo '<h1><a href=?id=' . $info['id'] . '>' . $info['title'] . $info['title'] .
'</a></h1>';
echo "<h4>" . $info['timestamp'] . " </h4>";
echo "<p> " . $info['body'] . " </p>";

echo '<img src=http://www.example.com' . $info['image'] .
'> <br/><br/><div class="line"></div>';
}
}
}
?>


Optimized the codes a bit, added backsticks. in mysql there are reserved words, so you need to add backsticks to rows and table names.

EDIT: you used a $row variable instead of $info in a few places, fixed my above code.

mastertooley
09-07-2009, 06:35 AM
Hi Zangeel,

Thanks for your time and your help, unfortunately the code you gave me gives me the following errors,

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in

'SELECT * FROM `bloggers` WHERE `id` = \'' . $id . ' ''';

if I change to this code:

'SELECT * FROM `bloggers` WHERE `id` = \'' .$id . ' ';

gives me a page without any posts.

Thank you for the help

regards

Zangeel
09-07-2009, 06:41 AM
Sorry, forgot to escape the single quote, try this:



<?php
if (isset($_GET['id'])) {

$id = (!empty($_GET['id'])) ? mysql_real_escape_string($_GET['id']) : null;
$sql = ($id === null) ? 'SELECT * FROM `bloggers` ORDER BY `id` DESC' :
'SELECT * FROM `bloggers` WHERE `id` = \'' . $id . '\'';

$data = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($data) != 0) {
while ($info = mysql_fetch_assoc($data)) {

echo '<h1><a href=?id=' . $info['id'] . '>' . $info['title'] . $info['title'] .
'</a></h1>';
echo "<h4>" . $info['timestamp'] . " </h4>";
echo "<p> " . $info['body'] . " </p>";

echo '<img src=http://www.example.com' . $info['image'] .
'> <br/><br/><div class="line"></div>';
}
}
}
?>

ruskin012
09-07-2009, 06:55 AM
I have also same trouble, but thanks for giving the solution.

mastertooley
09-07-2009, 07:06 AM
Hi Zangeel,

again Thank you , the code is working now but does not fetch any posts, it gives me a blank index page.... hmmm can't think why??

Any ideas??

Thnak you very very much for your help realy appreciated !

regards

Zangeel
09-07-2009, 07:25 AM
You're accessing the page like this right


http://site.com?file.php?id=1

or something similar right?

mastertooley
09-07-2009, 07:32 AM
Zangeel,

I'm on the index page, and it comes up the index page just with the HTML and CSS display but no data .

I hope I didn't confused you, the code is for the index page, and when i click on the link i want the data to display on its own on the page does this make sense??

Thank you

Zangeel
09-07-2009, 07:48 AM
The only way the information would display is if, the ID is invalid, or you're not setting the id variable in the URL.

So if you're code is on the index page for example, you need to access the code like this

yoursite.com/index.php?id
or yoursite.com/index.php?id=1

or something similar.

This way it'll run the php code. If you're not accesing the page with the ?id=.. portion it wont do anything on the php side.

Zangeel
09-07-2009, 08:22 AM
Wait I think I know what you're going for. Remove the check like this if you want them to be able to access the page without setting the get variable:



<?php

$id = (!empty($_GET['id'])) ? mysql_real_escape_string($_GET['id']) : null;

$sql = ($id === null) ? 'SELECT * FROM `bloggers` ORDER BY `id` DESC' :
'SELECT * FROM `bloggers` WHERE `id` = \'' . $id . '\'';

$data = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($data) != 0) {
while ($info = mysql_fetch_assoc($data)) {

echo '<h1><a href=?id=' . $info['id'] . '>' . $info['title'] . $info['title'] .
'</a></h1>';
echo "<h4>" . $info['timestamp'] . " </h4>";
echo "<p> " . $info['body'] . " </p>";

echo '<img src=http://www.example.com' . $info['image'] .
'> <br/><br/><div class="line"></div>';
}
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum