justlukeyou Posted February 13, 2011 Share Posted February 13, 2011 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>£ $price</center></div> <div class=\"productbuybutton\"><center><a href=\"$link\" target=\"_blank\" ><img src=/images/buybutton.png /></a></center></div></div></div>"; } ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 13, 2011 Share Posted February 13, 2011 You're attempting to use $id in the query string, but you've not assigned it a value. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted February 13, 2011 Author Share Posted February 13, 2011 Thanks, I thought I did that in my link when I search for the item: (example.com/ProductFeed/phpproductfeed.php?id=1) How do I assign it a value in the code? Quote Link to comment Share on other sites More sharing options...
blew Posted February 13, 2011 Share Posted February 13, 2011 $id = $_GET['id']; before the $sql = ... Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 13, 2011 Share Posted February 13, 2011 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. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted February 13, 2011 Author Share Posted February 13, 2011 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>£ $price</center></div> <div class=\"productbuybutton\"><center><a href=\"$link\" target=\"_blank\" ><img src=/images/buybutton.png /></a></center></div></div></div>"; } Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 13, 2011 Share Posted February 13, 2011 You never close your first if. EDIT: Actually you did, you just did it instead of closing the while. Check all your brackets. Also, see link in my signature Re: errors. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 13, 2011 Share Posted February 13, 2011 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>£ $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.'; } ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 13, 2011 Share Posted February 13, 2011 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 Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted February 13, 2011 Author Share Posted February 13, 2011 I tried it and without the "?id=3" ir reads $_GET['id'] is NOT set, or is NOT numeric. When I try it with "?id=3" I still get just the CSS. Could this mean that my db is set up wrong in some way? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 13, 2011 Share Posted February 13, 2011 No. It means you've not selected a record. What were you wanting to happen if no record is selected? Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted February 13, 2011 Author Share Posted February 13, 2011 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.