2

i am new to php, i am getting error as :

Notice: Undefined index: userID in echo "<a href='home.php?userID=".$row['userID'] . "'>".$row['userName']." ".$row['userEmail']."</a><br/>"; 

home.php

$sql = 'SELECT userName, userEmail FROM tbl_users';
$stmt = $dbCon->prepare($sql);
$stmt->setFetchMode(PDO::FETCH_ASSOC); //allows you to refer to them by column name rather than by number
$stmt->execute();
while ($row = $stmt->fetch())
{
    echo "<a href='home.php?userID=".$row['userID'] . "'>".$row['userName']." ".$row['userEmail']."</a><br/>"; //builds the link
}

i followed this link and add the below code in home.php & signup.php , but it didt worked for me, where i done mistake ?

//Initializing variable
$userID = "";                        
//isset()
$userID = isset($_POST['userID']) ? $_POST['userID'] : '';
//empty()
$userID = !empty($_POST['userID']) ? $_POST['userID'] : '';

Edit

this question is not duplicate because i followed the answer in that link, but it did't worked for me.

Community
  • 1
  • 1
  • `print_r($row)` – u_mulder Oct 13 '16 at 09:17
  • 3
    You didn't select `userID` in your sql. You select only `userName, userEmail` – S.I. Oct 13 '16 at 09:18
  • @u_mulder Thansk a lot for support..... –  Oct 13 '16 at 09:19
  • @S.I. Thanks for support..... –  Oct 13 '16 at 09:19
  • please tell why downvotes, so that i can correct it, i am new to php..... –  Oct 13 '16 at 09:20
  • @u_mulder i followed that links answer , still it didt worked for me, please `reopen` it..... –  Oct 13 '16 at 09:24
  • 1
    I don't see anything here that warrants a downvote tbh; you've at least done the search and found the defacto canonical answer and seen that it doesn't quite fit your problem which is more than many people. The relevant code is there and it was easy for a new pair of eyes to spot the problem (hence the quick answers)... seems like a good enough question to me. – CD001 Oct 13 '16 at 09:26
  • 3
    I think the people who are downvoting are the ones who haven't read properly... I upvoted because you've done your research, and your question was well formed and contained the relevant parts. As evidenced by the answers you have gotten. – ChristianF Oct 13 '16 at 09:30
  • Why are you adding the `isset()` checks on `$_POST`? Your error message complains about `$row`. – Barmar Oct 13 '16 at 10:46
  • @Barmar as in this [link](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) , they mentioned , i tried that, as that didt worked for me, i removed it now, thanks.... –  Oct 13 '16 at 10:48
  • I understand that, but you have to think about what you're doing, not just blindly copy code. If the error message is about variable $a, making a fix to variable $b won't help. – Barmar Oct 13 '16 at 10:50
  • @Barmar thanks again, you are absolutely right, still i did't have much knowledge to understand things in php as i just started to learn, i will avoid these kind of mistakes in future..... –  Oct 13 '16 at 10:51

2 Answers2

1

Change SQL query to select the UserID along with other two fields.

It should look something like this,

$sql = 'SELECT userID,userName, userEmail FROM tbl_users';

I'm assuimg the column name is userID, if not change it to the original column name and give it an alias to access it with other elements.

So if your original column name is just ID then your might look something like this,

$sql = 'SELECT ID userID,userName, userEmail FROM tbl_users';
Alok Patel
  • 7,842
  • 5
  • 31
  • 47
-1

You don't have an element with key userID in your $row array. And that's because you don't select it in your query:

'SELECT userName, userEmail FROM tbl_users'

So, do it like this:

'SELECT userID, userName, userEmail FROM tbl_users'

And userID will be fetched with userName and userEmail.

Edit

The reason why your variable initialization did not work is because your where declaring string variable $userID, while you're echoing a string that's an element of the array $row, so it's a different variable.

Georgy Ivanov
  • 1,573
  • 1
  • 17
  • 24