Jump to content

Strange isset($_GET problem


justlukeyou

Recommended Posts

I am trying to do isset($_GET but I have a bizarre problem come up.  I am using "id" as one of my fields but when I try to use it in the code it says it is not recognised.  However I use this field quite widely.

 

The code its coming up with is "Undefined variable: id" but I cant see why it has a problem with id.

 

My code is:

 

<?php

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

$sql = mysql_query("SELECT * FROM productfeed WHERE id='$id' LIMIT 1");

}
{


while($row = mysql_fetch_array($sql))

$id = $row['id'];
$image = $row['awImage'];
$link = $row['link'];
$description = $row['description'];
$fulldescription = $row['fulldescription'];
$price = $row['price'];


echo "<div class=\"productdisplayshell\"> <div class=\"productdisplayoutline\"> <div class=\"productborder\"><center>  <a href=\"$link\"  target=\"_blank\" ><img src=\"$image\" /></a> </center> </div></div> <div class=\"productdescriptionoutline\"><div class=\"productdescriptionbox\">  <a href=\"$link\"  target=\"_blank\" >$description</a> </div><div class=\"productfulldescriptionbox\">  $fulldescription </div></div> <div class=\"productpriceoutline\">  <div class=\"productpricebox\"><center>&#163; $price</center></div>  <div class=\"productbuybutton\"><center><a href=\"$link\"  target=\"_blank\" ><img src=/images/buybutton.png /></a></center></div></div></div>";

} 




?>

Link to comment
https://forums.phpfreaks.com/topic/227534-strange-isset_get-problem/
Share on other sites

Since it's form data, you should first validate it and sanitize it for use in the query. This assumes that $_GET['id'] is expected to be an integer value.

 

f( isset($_GET['id']) && ctype_digit($_GET['id']) ) { // validate that $_GET['id'] is set, and contains only numeric characters
$id = (int) $_GET['id']; // cast value as an integer, and assign to $id
$sql = mysql_query("SELECT * FROM productfeed WHERE id = $id LIMIT 1"); // numeric values shouldn't be quoted in query strings.

 

 

 

Thanks, this is my code now but I have a white screen of death without any errors:

 

<?php

if( isset($_GET['id']) && ctype_digit($_GET['id']) ) { // validate that $_GET['id'] is set, and contains only numeric characters
$id = (int) $_GET['id']; // cast value as an integer, and assign to $id
$sql = mysql_query("SELECT * FROM productfeed WHERE id = $id LIMIT 1"); // numeric values shouldn't be quoted in query strings.

while($row = mysql_fetch_array($sql))

$id = $row['id'];
$image = $row['awImage'];
$link = $row['link'];
$description = $row['description'];
$fulldescription = $row['fulldescription'];
$price = $row['price'];


echo "<div class=\"productdisplayshell\"> <div class=\"productdisplayoutline\"> <div class=\"productborder\"><center>  <a href=\"$link\"  target=\"_blank\" ><img src=\"$image\" /></a> </center> </div></div> <div class=\"productdescriptionoutline\"><div class=\"productdescriptionbox\">  <a href=\"$link\"  target=\"_blank\" >$description</a> </div><div class=\"productfulldescriptionbox\">  $fulldescription </div></div> <div class=\"productpriceoutline\">  <div class=\"productpricebox\"><center>&#163; $price</center></div>  <div class=\"productbuybutton\"><center><a href=\"$link\"  target=\"_blank\" ><img src=/images/buybutton.png /></a></center></div></div></div>";

} 



 

It probably just isn't returning any results.

 

<?php
if( isset($_GET['id']) && ctype_digit($_GET['id']) ) { // validate that $_GET['id'] is set, and contains only numeric characters
$id = (int) $_GET['id']; // cast value as an integer, and assign to $id
$query = "SELECT * FROM productfeed WHERE id = $id LIMIT 1";
if( !$sql = mysql_query($query) ) { // numeric values shouldn't be quoted in query strings.
	echo "Query: $query<br>Failed with error: " . mysql_error() . '<br>';
} else {
	while($row = mysql_fetch_array($sql))

	$id = $row['id'];
	$image = $row['awImage'];
	$link = $row['link'];
	$description = $row['description'];
	$fulldescription = $row['fulldescription'];
	$price = $row['price'];




	echo "<div class=\"productdisplayshell\"> <div class=\"productdisplayoutline\"> <div class=\"productborder\"><center>  <a href=\"$link\"  target=\"_blank\" ><img src=\"$image\" /></a> </center> </div></div> <div class=\"productdescriptionoutline\"><div class=\"productdescriptionbox\">  <a href=\"$link\"  target=\"_blank\" >$description</a> </div><div class=\"productfulldescriptionbox\">  $fulldescription </div></div> <div class=\"productpriceoutline\">  <div class=\"productpricebox\"><center>&#163; $price</center></div>  <div class=\"productbuybutton\"><center><a href=\"$link\"  target=\"_blank\" ><img src=/images/buybutton.png /></a></center></div></div></div>";

}
} else {
echo '$_GET[\'id\'] is NOT set, or is NOT numeric.';
}
?>

You should check to make sure the query worked before using the results:

<?php
$q = "SELECT * FROM productfeed WHERE id = $id LIMIT 1"; // numeric values shouldn't be quoted in query strings.
$sql = mysql_query($q) or die("Problem with the query: $q<br>" .  mysql_error());
?>

 

Also, you have  syntax error in the PHP associated with the while loop, since you don't put the body of the loop within curly brackets "{ }".

 

Ken

I see so if some adapts the search, the message comes up.

 

Im using a tuturial from YouTube from my iPhone, the video does include the else option to display messages if products aren't available but it does put numeric values in brackets.

 

It also sanitises the id but comes up with lots of errors.

 

I cant see why it now gives me a white screen though.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.