user652792
user652792

Reputation:

Issues with dynamically generated web pages

Please I need your help with my script. I'm puting a link to old news articles in a sidebar and making it clickable. The page it's coming from (header.php) has the GET id in the URL, so the page receiving it also checks for the value of the GET. It displays fine when I click the old news article in the sidebar.

The problem I'm having is that, whenever I want to view the current article on the the About.php page I get Undefined Index id

Please how can I solve this issue, so that my script works well for displaying old articles and also the current news article.

Thanks

about.php

<?php

$id = $_GET['id'];
$past = mysql_query( "SELECT * FROM about WHERE about_id = '".$id."'") or die(mysql_error()); 
$row = mysql_fetch_array($past);

echo "<h2>";
echo $row1['about_head'];
echo "</h2>";
echo "<p>";
echo $row1['about_content'];
echo "</p>";

?>

Header

<?php

$past = mysql_query("SELECT * FROM about") or die(mysql_error());                    
while($row = mysql_fetch_array($past))
    echo "<a href=\"http://127.0.0.1/church/About.php?id=$row[about_id]\"> $row[about_head].</a><br/>";

?>

Upvotes: 1

Views: 171

Answers (2)

kuba
kuba

Reputation: 7389

When you have this code:

      $id = $_GET['id'];

you are retriving an item called "id" from the array called $_GET (which holds all GET parameters). However when this parameter "id" is not present, PHP emits a warning. To get rid of it, replace the line with:

$id = "";
if (isset($_GET["id"])) $id = $_GET["id"];

or shortly:

$id = isset($_GET["id"]) ? $_GET["id"] : "";

which first asks whether the parameter is present, and if it's not, gives an empty string. If you expect the $id variable to be an integer, you might instead want to use zero instead of an empty string:

$id = isset($_GET["id"]) ? (int)$_GET["id"] : 0;

this also casts the passed parameter to "int", so you have a guarantee that it is not a string (possibly containing malicious data).

Upvotes: 2

Matthew Blancarte
Matthew Blancarte

Reputation: 8301

Something like this should work:

if( array_key_exists( 'id', $_GET ) )
{
  //do your code in here
}
else
{
  //fallback to scenario in which $_GET['id'] isn't set in the url
}

Upvotes: 0

Related Questions