Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 undefined index=id error

Author  Topic 

jasper351
Starting Member

1 Post

Posted - 2009-07-21 : 10:03:12
a newbie here to php/sql and trying to figure this one out
won't lie this was the hardest course I've taken so far
I'm sure some might recognize the content but I did finish the course/and only missed one question, but just can't get this
spent 3 weeks going over the class/notes/discussion area but can't figure it out I believe it has to do with the word "ID"
tried using the little trick of "echo "<br/>" . $query."<br/>"
in between the query created and the statement
also tried the trick
$query=mysql_query($query)or die(mysql_error());
and I'm not sure if all the needed info has been put in below for others to assist
thanks for any advice/help given

PROBLEM
when in localhost/bring up the page/this is what shows up

SHOWRECIPE.INC.PHP

Notice: Undefined index: id in C:\wamp\www\recipe\showrecipe.inc.php on line 5
Could not find recipe
-----------------------------------
here is my page in the www folder

<?php
$con = mysql_connect("localhost", "test", "test") or die('Could not connect to server');
mysql_select_db("recipe", $con) or die('Could not connect to database');

$recipeid = $_GET['id'];

$query = "SELECT title,poster,shortdesc,ingredients,directions FROM recipesWHERE recipeid = $recipeid";

$result = mysql_query($query) or die('Could not find recipe');
$row=mysql_fetch_array($result, MYSQL_ASSOC) or die('No records retrieved');
$title = $row['title'];
$poster = $row['poster'];
$shortdesc = $row['shortdesc'];
$ingredients = $row['ingredients'];
$directions = $row['directions'];

$ingredients = nl2br($ingredients);
$directions = nl2br($directions);

echo "<h2>$title</h2>\n";

echo "by $poster<br><br>\n";
echo $shortdesc . "<br><br>\n";
echo "<h3>Ingredients:</h3>\n";
echo $ingredients . "<br><br>\n";

echo "<h3>Directions:</h3>\n";
echo $directions . "\n";
echo "<br><br>\n";

$query = "SELECT count(commentid) from comments where recipeid = $recipeid";
$result = mysql_query($query);
$row=mysql_fetch_array($result);
if ($row[0] == 0)
{
echo "No comments posted yet.  \n";
echo "<a href=\"index.php?content=newcomment&id=$recipeid\">Add a comment</a>\n";
echo "   <a href=\"print.php?id=$recipeid\" target=\"_blank\">Print recipe</a>\n";
echo "<hr>\n";
} else
{
$totrecords = $row[0];
echo $row[0] . "\n";
echo " comments posted.  \n";
echo "<a href=\"index.php?content=newcomment&id=$recipeid\">Add a comment</a>\n";
echo "   <a href=\"print.php?id=$recipeid\" target=\"_blank\">Print recipe</a>\n";
echo "<hr>\n";
echo "<h2>Comments:</h2>\n";

if (!isset($_GET['page']))
$thispage = 1;
else
$thispage = $_GET['page'];

$recordesperpage = 5;
$offset = ($thispage - 1) * $recordsperpage;
$totpages = ceil($totrecords / $recordsperpage);

$query = "SELECT date,poster,comment FROM comments WHERE recipeid = $recipeid ORDER BY commentid desc limit $offset,$recordsperpage";
$result = mysql_query($query) or die('Could not retrieve comments');
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$date = $row['date'];
$poster = $row['poster'];
$comment = $row['comment'];
$comment = nl2br($comment);

echo $date . " - posted by " . $poster . "\n";
echo "<br>\n";
echo $comment . "\n";
echo "<br><br>\n";
}

if ($thispage > 1)
{
$page = $thispage - 1;
$prevpage = "<a href=\"index.php?content=showrecipe&id=$recipeid&page=$page\">Previous</a>";
} else
{
$prevpage = "Previous";
}
$bar = '';
if($totpages > 1)
{
for($page = 1; $page <= $totpages; $page++)
{
if($page == $thispage)
{
$bar.=" $page ";
}else
{
$bar .= " <a href=\"index.php?content=showrecipe&id=$recipeid&page=$page\">$page</a> ";
}
}
}

if ($thispage < $totpages)
{
$page = $thispage + 1;
$nextpage = " <a href=\"index.php?content=showrecipe&id=$recipeid&page=$page\">Next</a>";
} else
{
$nextpage = "Next";
}

echo "GoTo: " . $prevpage . $bar . $nextpage;
}
?>


again thanks for any help/advice

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-07-21 : 10:46:57
ok, 1st this is a SQL Server forum (different from MySQL) so you might have better luck on a PHP/MySQL forum...BUT

These two lines of code are the interesing part:

$recipeid = $_GET['id'];

$query = "SELECT title,poster,shortdesc,ingredients,directions FROM recipes WHERE recipeid = $recipeid";


The first one is populating the variable %recipeid with the "id" passed in the querystring. Seems you are passing the value "ID" to it. This should be a number by the looks of things.

also assuming this: recipesWHERE is just a typo and not in your code like that...?
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-07-21 : 11:57:50
You can google this: Notice: Undefined index: id in C:\wamp\www
and you will find many answers.

I think $_GET means that id AND value should come with the url but in your case it doesn't.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-07-21 : 12:21:24
quote:
Originally posted by webfred

I think $_GET means that id AND value should come with the url but in your case it doesn't.


$recipeid = $_GET['id']; means assign the value in the querystring to the variable $recipeid.

so if the URL was
www .mysite.com?id=3
then $recipeid gets the value 3.

In this case, it isn't happening.
Go to Top of Page
   

- Advertisement -