Jump to content

Recommended Posts

Hi all,

 

I am fairly new to php.  I am learning php by a book.'

 

I am getting an undefined index error. With the following code in dreamweaver.

 

<?php

  if

  ($row_rsCoffee['image'] == "")

  {

  echo '<img src="images/missing_image.jpg" />';

  }

  else

  {

  echo '<img src="images/' . $row_rsCoffee['image'] . '" />';

  }

  ?>

 

The line the error is at: ($row_rsCoffee['image'] == "") --> Notice: Undefined index: image in C:\wamp\www\coffee\coffeevarieties.php

 

Can anyone help with this>?

 

Thanks in advance.

 

Alexander Lejuez

Link to comment
https://forums.phpfreaks.com/topic/257983-help-with-undefined-index/
Share on other sites

You are not getting an error - you are getting a "Notice". In a production environment (i.e. live site you would have error reporting set such that notices are not displayed.. But, in a development environment you want error reporting set to ALL.

 

In this specific instance your condition statement is trying to reference an array value where no such index exists (i.e. it is not defined)

if ($row_rsCoffee['image'] == "")

 

So the array $row_rsCoffee does exists, but there is no value defined for the index 'image'. The notices are there to let you know that you may have made a mistake (e.g. used the wrong index name).

 

If error reporting was set to not report notices, that condition would try to compare a nonexistent variable to a null string. Using the loose comparison operator that would result in true. Assuming that is the outcome you want, it would work, but it is sloppy IMO. You would want to use isset() but not like floridaflatlander suggested.

 

Assuming you want the condition to be true if the variable is not set OR if the variable is set and an empty string, I would use this

if (!isset($row_rsCoffee['image']) || $row_rsCoffee['image'] == "")

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.