0

I am trying to understand why I get this error:

Notice: Undefined index: id in E:\xampp\htdocs\CVToolGismo\addCV.php on line 472

The part of the code that I get the error is this:

if($query = mysql_query("SELECT job_title,company,website,start_date,end_date,start_year,end_year,work_history FROM work WHERE username='$username' order by id asc limit 1") or die(mysql_error())){
                  if(mysql_num_rows($query)>0){
                    while($row = mysql_fetch_array($query)) {
                         $id = $row['id'];
                    }
                  }
                  else{
                      echo '<script>ErrorMessage()</script>';
                  }
              }
halfer
  • 19,824
  • 17
  • 99
  • 186
Waaaaat
  • 634
  • 3
  • 14
  • 29
  • 1
    Because you’re not selecting a column named `id` in your statement …? – CBroe Mar 12 '15 at 22:12
  • @EmmadKareem it is a compination of the id and the username – Waaaaat Mar 12 '15 at 22:15
  • This is not a duplicate of that question. I'm sure it's a duplicate of *some* question, but not *that* one. – Ignacio Vazquez-Abrams Mar 12 '15 at 22:19
  • @IgnacioVazquez-Abrams Please tell me why this should not be a dupe?! He wants to access an undefined index in an array, from my perspective this is an exact dupe – Rizier123 Mar 12 '15 at 22:22
  • @IgnacioVazquez-Abrams it is not a duplicate question – Waaaaat Mar 12 '15 at 22:23
  • @Rizier123: That question covers the general case of the index not existing. The issue in this question is that the asker has deliberately crafted their code (and specifically their SQL query) to invoke the error. – Ignacio Vazquez-Abrams Mar 12 '15 at 22:26
  • @IgnacioVazquez-Abrams But don't you think with a little debugging and the use of google he would have spotted that? Also *I'm sure it's a duplicate of some question* If you know about a dupe which I'm not aware of, why do you answer a duplicate question? – Rizier123 Mar 12 '15 at 22:28

2 Answers2

1

Your query doesn't have a id column.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

In your query you haven't actually selected the id column. You must query the id column in the SELECT statement to use it.

Ben Butler
  • 11
  • 2