-9

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 "<dt>Name:</dt><dd>" . $row["name"] . "</dd>";
    echo "<dt>Username:</dt><dd>" . $row["username"] . "</dd>";
    echo "<dt>Rollno:</dt><dd>" . $row["rollno"] . "</dd>";

}
?>

the displayed error messages are:

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
Vishnu R
  • 1,831
  • 3
  • 25
  • 45
Franklin Vaz
  • 13
  • 1
  • 2
  • 9
  • means mysql_query($strSQL); is returning false, check if your query is valid – Deepanshu Goyal Apr 11 '13 at 05:12
  • print $_GET["id"] and check get something? – Bhumi Shah Apr 11 '13 at 05:13
  • you should always use isset() before using $_GET and $_POST variables – Deepanshu Goyal Apr 11 '13 at 05:14
  • 1
    execute `SELECT * FROM login WHERE id=1` in mysql query browser, and ssee if it executes properly. Also, echo $_GET['id'] and see if it returns a valid value – karthikr Apr 11 '13 at 05:14
  • There is no key named `id` found in the query string of the URL. – asprin Apr 11 '13 at 05:15
  • @AndyLester no. This is my project, i've got a doubt in this and hence asking. It is not duplicated from any other. Hope u can understand. – Franklin Vaz Apr 11 '13 at 05:23
  • 1
    This code is extremely insecure and can be affected by mysql injections. Firstly mysql_ functions are dead and you shouldn't be using them over something like PDO. Secondly you are just accepting whatever that $_GET is without even sanitizing it meaning someone could really mess up your server if they know what they are doing. – Lemon Drop Apr 11 '13 at 05:34
  • 1
    Why can you not use `or die(mysql_error());` where you execute your query and see the error for yourself? Im surprised nobody advised it yet – Hanky Panky Apr 11 '13 at 05:48
  • Iam tryin to do the example3 from this web tutorial. Exactly the same. trying to implement it in my project. i don know where i am going wrong?... please hlep me. http://www.html.net/tutorials/php/lesson20.php – Franklin Vaz Apr 11 '13 at 06:49
  • @karthikr. When i try to use id=1, it works. but why $_GET["id"] is not working?? the way i hav used it here is correct or incorrect? please help – Franklin Vaz Apr 11 '13 at 06:50
  • @Deepanshu: please tell me this i right or wrong! $strSQL = "SELECT * FROM login WHERE id =" . $_GET['id'] ; $rs = mysql_query($strSQL); – Franklin Vaz Apr 11 '13 at 07:04
  • are you sure there is some id you are sending with your url ? can you post here the url that opens up ? – Deepanshu Goyal Apr 11 '13 at 07:22
  • 1
    the only thing that could be creating problem is `$_GET["id"]` – Deepanshu Goyal Apr 11 '13 at 07:26
  • @Deepanshu: http://localhost/phploginsession/person.php?id%20=%2010 this is the url... even i can understand this get id is th prob. – Franklin Vaz Apr 11 '13 at 07:31
  • @Deepanshu: jus see th example3 in this website. This is wat im tryin to do. http://www.html.net/tutorials/php/lesson20.php – Franklin Vaz Apr 11 '13 at 07:33
  • @FranklinVaz the problem is in your url, when you are calling it, just remove the extra spaces in your code, when the url is called, your url should be like this : localhost/phploginsession/person.php?id=10, currently it localhost/phploginsession/person.php?id = 10, if you can understand the difference – Deepanshu Goyal Apr 11 '13 at 08:37
  • @Deepanshu: I tried tat too. now my url is localhost/phploginsession/person.php?id=6 but why still not working? can i hav your mail id? i shall mail you my wrk. please help! if i remov $_GET["id"] and put id=1 or any id..it works perfectly... watz wrong? – Franklin Vaz Apr 11 '13 at 12:29

3 Answers3

0
//list.php
<html>
    <head>
    <title>Retrieve data from the database</title>
    </head>
    <body>

    <ul>

    <?php
    // Connect to database server
    mysql_connect("mysql.myhost.com", "user", "sesame") or die (mysql_error ());

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

    // SQL query
    $strSQL = "SELECT * FROM people ORDER BY FirstName DESC";

    // 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['FirstName'] . " " . $row['LastName'];

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

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

      }

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

    </ul>
    </body>
    </html>
Dishu
  • 3
  • 4
  • Iam tryin to do the example3 from this web tutorial. Exactly the same. trying to implement it in my project. i don know where i am going wrong?... please hlep me. http://www.html.net/tutorials/php/lesson20.php – Franklin Vaz Apr 11 '13 at 06:48
  • When i try var_dump($_GET['id']) it again shows me the error message :( – Franklin Vaz Apr 11 '13 at 06:52
  • It shows you error message because you are not getting index 'id' in $_GET['id'].Check your data array from where you are getting this id index. – Dishu Apr 11 '13 at 11:05
  • Now try to use it by replacing your list.php code – Dishu Apr 11 '13 at 11:26
0
// Connect to database server
$con=mysql_connect("localhost", "root", "") or die (mysql_error ());
// Select database
mysql_select_db("lms",$con) 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= 1 ";
$rs = mysql_query($strSQL,$con);

// (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>";

}

can u try this .. passing db object

Joe
  • 618
  • 1
  • 8
  • 17
  • Not passing a db object won't affect the outcome unless of course there are multiple db connections which I doubt is the case with the OP – asprin Apr 11 '13 at 05:41
  • @asprin yea exactly... it din work... – Franklin Vaz Apr 11 '13 at 06:28
  • Iam tryin to do the example3 from this web tutorial. Exactly the same. trying to implement it in my project. i don know where i am going wrong?... please hlep me. http://www.html.net/tutorials/php/lesson20.php – Franklin Vaz Apr 11 '13 at 06:46
0

Your query is broken. After the line $rs = mysql_query($strSQL); add this: var_dump($rs) to see what's going on and use mysql_error() to debug it.

f.ardelian
  • 6,172
  • 7
  • 34
  • 50
  • i did used this... it says boolean false, and the error still remains.... please go through 17th and 21st line for any specific error in t. somethin must be there. please help me fix my prob – Franklin Vaz Apr 11 '13 at 12:12
  • Ah, now I see. You don't have $_GET['id']. You either submit a form to this page using POST and you should use $_POST, or you aren't using GET properly. Do a `print_r($_GET)` to check for GET variables and `print_r($_POST)` to check for POST variables. Either way, **YOUR IS IMPORTANT: YOUR CODE IS PRONE TO SQL INJECTION**. Please read about SQL injection before you put this in production! – f.ardelian Apr 11 '13 at 16:06
  • please, tell me how rid of that??? – Franklin Vaz Apr 12 '13 at 07:15
  • @FranklinVaz: I told you to read about SQL injection and you ask me about it. Did you even touch a search engine to find more information or do you expect me to explain every little detail in this comment? Or do you expect me to give you all the links from the front page of Google? 1. StackOverflow http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string 2. Straight from php.net: http://php.net/manual/en/security.database.sql-injection.php 3. Google: http://goo.gl/Km2a8 – f.ardelian Apr 12 '13 at 11:21