Please help me with this php error message

:frowning: :frowning: ??? :frowning: Please tell me whats wrong with this? im getting error in line 17 & 21, please help… The purpose of this program is to fetch & display the details of the user from db.

<?php // Connect to database server mysql_connect("localhost", "root", "") or die (mysql_error ()); // Select database mysql_select_db("lms") or die(mysql_error()); // (Line 17) Get data from the database depending on the value of the id in the URL $strSQL = "SELECT * FROM login WHERE id=" . $_GET["id"]; $rs = mysql_query($strSQL); // (Line 21) Loop the recordset $rs while($row = mysql_fetch_array($rs)) { // Write the data of the person echo "
Name:
" . $row["name"] . "
"; echo "
Username:
" . $row["username"] . "
"; echo "
Rollno:
" . $row["rollno"] . "
"; } ?>

the displayed error message is:

  • Undefined index: id in C:\wamp\www\phploginsession\person.php on line 17
  • mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\phploginsession\person.php on line 21

:frowning: :frowning: :frowning: :frowning: :frowning:

Undefinded index: id…

This is telling you the $_GET[‘id’] has no value.

Hope that points you in the right direction.

Red :wink:

PS:
As a side note, you should stop using the mysql functions as they are now depreciated and will be removed at some point in the future rendering your current scripts useless.

Add an ‘i’ on the end to use the new extension like so mysqli_connect, mysqli_query etc…
Reference here.

As Redscouse says, $_GET[‘id’] is NULL when the page first loads if you don’t have a value in the URL parameter id=, for example: Somepage.php, rather than Somepage.php?id=5.

So you need to check that $_GET[‘id’] contains something before executing the subsequent script.

For example:

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

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

// (Line 17) Get data from the database depending on the value of the id in the URL
$strSQL = "SELECT * FROM login WHERE id=" . $_GET["id"];
$rs = mysql_query($strSQL);

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

    // Write the data of the person
    echo "<dt>Name:</dt><dd>" . $row["name"] . "</dd>";
    echo "<dt>Username:</dt><dd>" . $row["username"] . "</dd>";
    echo "<dt>Rollno:</dt><dd>" . $row["rollno"] . "</dd>";
}

}
else{
echo “No ID supplied”;
}
?>
[/php]

I would also check that the value of id is numeric and that that the query returns a result before trying to loop through it, something like this:

[php]
if (isset($_GET[“id”])){
$intID=$_GET[“id”];
}
else{$intID=“x”;
}

if (is_numeric($intBID)==true){
// Connect to database server
mysql_connect(“localhost”, “root”, “”) or die (mysql_error ());

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

    // (Line 17) Get data from the database depending on the value of the id in the URL
    $strSQL = "SELECT * FROM login WHERE id=" . $_GET["id"];
    $rs = mysql_query($strSQL);
     if (mysql_num_rows($rs)>0){

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

            // Write the data of the person
            echo "<dt>Name:</dt><dd>" . $row["name"] . "</dd>";
            echo "<dt>Username:</dt><dd>" . $row["username"] . "</dd>";
            echo "<dt>Rollno:</dt><dd>" . $row["rollno"] . "</dd>";
         }
    }
}
else{
    echo "No ID supplied, or ID not valid";
}

}
[/php]

That way, if the URL parameter is empty or has been tampered with it give a nice error message rather than a PHP error message.

While we’re on the subject, you shouldn’t really retrieve User information with $_GET because it’s visible in the browser address bar, so it can be tampered with to retrieve a different user’s information. Use $_POST instead.

Note, I’ve missed a semicolon of that error message line. :-[

Why can’t you edit posts on this forum? ???

No clue :slight_smile:

Try [php]$_REQUEST[‘id’];[/php]

Hey! you! awesome man! i just forgt to use (if isset function)… you are brilliant man! thanks alot… it worked… 8) 8) :wink: :wink:

You’re welcome. :smiley:

don’t forget the ‘i’
it will become your best friend sooner rather than later… :wink:

[hr]how to bring the retrieved data inside html (The variables wil hav to vary according to the id i supply)[hr] just to echo the results inside this div???

[s]Franklin[/s]
[s]gdcfrank[/s]
[s]123456[/s]

Thank you in advance

Sponsor our Newsletter | Privacy Policy | Terms of Service