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

 

 

 

Link to comment
Share on other sites

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>";

} 



 

Link to comment
Share on other sites

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.';
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.