Login | Register | FAQ
Anonymous

Lesson 20 - undefined id error message.

+ Post a reply

6 posts Page 1 of 1


Lesson 20 - undefined id error message.

by Dogmalone » Tue Apr 03, 2012 2:59 pm

I keep getting Notice: Undefined index: id for:
$strSQL = "SELECT * FROM people WHERE id=" . $_GET["id"];

and, also

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean for:
while($row = mysql_fetch_array($rs)) {

Please help me. I am stuck.
User avatar

Dogmalone

  • Posts: 4
  • Joined: Tue Feb 21, 2012 4:38 am

Re: Lesson 20 - undefined id error message.

by XainPro » Wed Apr 04, 2012 5:08 am

Well Dogmalone,
Notice is not an error its just caution that you can code better then your existing code.
in any programing language when you are trying to use any variable is should be declare before you use it.
like you have to declare a variable
$variable = "123";
to use it
in any further coding if you do not define the variables prior to use then other programing languages give fatal error but PHP is nice it only gives the notice about it and does not halt the execution of the code
in your code there are 2 problems 1st you need to check the
$_GET["id"]
before using it in query
like
Code: Select all
if(isset($_GET["id"])) { $theId = $_GET["id"]; }
else {
die("Please Enter Id or the ID is empty !");
}

and you query becomes like this
$strSQL = "SELECT * FROM people WHERE id=" . $theId. "";
because the $_GET["id"] is not entered or does not have any value that's why the 2nd problem arises.
check in your form element do you set the method to post ?
and the name of your input element is id ?
this will resolve your problem.
Regards,
Xain Pro
User avatar

XainPro

  • Posts: 3661
  • Joined: Fri Feb 17, 2012 8:10 pm

Re: Lesson 20 - undefined id error message.

by Dogmalone » Wed Apr 04, 2012 11:09 am

The suggestion you gave me does not seem to work. the variable ID is suppose to be generated by the previous form (List.php) in lesson #20. The form (person.php) is suppose to pick up that variable which it is not. The problem now is that it does not find the variable. it just renders me the message "Please Enter Id or the ID is empty !"
User avatar

Dogmalone

  • Posts: 4
  • Joined: Tue Feb 21, 2012 4:38 am

Re: Lesson 20 - undefined id error message.

by XainPro » Thu Apr 05, 2012 4:13 am

yah it means
1.The name of you text field is not ID or
2.The method of your form action is not get or
3.Please do check its case is it upper case or lowercase
i.e
you are using uppercase in html form and lowercase in php file ?
<input type="text" name="id" />

$_GET["ID"]
if the problem still exist please paste your code here !
User avatar

XainPro

  • Posts: 3661
  • Joined: Fri Feb 17, 2012 8:10 pm

Re: Lesson 20 - undefined id error message.

by Dogmalone » Thu Apr 05, 2012 10:31 am

[color=#FF0000]Addressbook1.php works, but Addressbook2.php does not respond.[/color]
This is my code for Addressbook1.php

<head>
<title>Lab record</title>
</head>
<body>

<ul>

<?php
// Connect to database server
mysql_connect("localhost", "root", "") or die (mysql_error ());

// Select database
mysql_select_db("opt") or die(mysql_error());

// SQL query
$strSQL = "SELECT * FROM lab_jobs ORDER BY id ASC";

// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);

// Loop the recordset $rs
while($row = mysql_fetch_array($rs)) {

// Name of the person
$strName = $row['id'] ." " .$row['lastname'] . " " . $row['firstname'];

// Create a link to person.php with the id-value in the URL
$strLink = "<a href = 'addressbook2.php? id = " . $row['id'] . "'>" . $strName . "</a>";

// List link
echo "<li>" . $strLink . "</li>";

}

// Close the database connection
mysql_close();
?>

</ul>
</body>

This is my code for Addressbook2.php

<html>
<head>
<form action='Addressbook2.php' method='GET'>
<title>Retrieve data from database</title>
</head>
<body>

<dl>

<?php
if ( isset($_GET["id"])){$id=$_GET["id"];}else{$id="";}
// Connect to database server
mysql_connect("localhost", "root", "") or die (mysql_error());

// Select database
mysql_select_db("opt") or die(mysql_error());

// Get data from the database depending on the value of the id in the URL
$strSql = "SELECT * FROM lab_jobs WHERE id = ".$_GET['id'];


$rs = mysql_query($strSql);

// Loop the recordset $rs
while($row = mysql_fetch_array($rs)) {

// Write the data of the person
echo "<dt>Phone:</dt><dd>" . $row['firstname'] . "</dd>";

}
?>

</dl>
<p><a href="Addressbook1.php">Return to the list</a></p>

</body>
</form>
</html>
User avatar

Dogmalone

  • Posts: 4
  • Joined: Tue Feb 21, 2012 4:38 am

Re: Lesson 20 - undefined id error message.

by bruno4eva » Tue Apr 01, 2014 8:36 am

For the second page where the contacts are going to be displayed, use a code similar to this
//
<?php
mysql_connect("localhost","root","root");
mysql_select_db("mydatabase");
$sql = "SELECT * FROM people WHERE id=$_GET[id]";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo "<dt>Name:</dt><dd>" . $row["firstname"] . " " . $row["lastname"] . "</dd>";
echo "<dt>Phone:</dt><dd>" . $row["phone"] . "</dd>";
echo "<dt>Birthdate:</dt><dd>" . $row["birthdate"] . "</dd>";
?>
//

If you have noticed i have eliminated the while(loop)that was causing the error..
Again this is how the query should run to fetch id
//
$sql = "SELECT * FROM people WHERE id=$_GET[id]";
//
User avatar

bruno4eva

  • Posts: 1
  • Joined: Tue Apr 01, 2014 8:31 am


+ Post a reply

Page 1 of 1