alexanderlejuez Posted February 29, 2012 Share Posted February 29, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/257983-help-with-undefined-index/ Share on other sites More sharing options...
floridaflatlander Posted February 29, 2012 Share Posted February 29, 2012 Try if (isset($row_rsCoffee['image'] == "")) Quote Link to comment https://forums.phpfreaks.com/topic/257983-help-with-undefined-index/#findComment-1322329 Share on other sites More sharing options...
floridaflatlander Posted February 29, 2012 Share Posted February 29, 2012 I don't do it like this so it might be if (isset($row_rsCoffee['image'])) just play with it, it's along these lines anyway. Quote Link to comment https://forums.phpfreaks.com/topic/257983-help-with-undefined-index/#findComment-1322333 Share on other sites More sharing options...
Psycho Posted February 29, 2012 Share Posted February 29, 2012 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'] == "") Quote Link to comment https://forums.phpfreaks.com/topic/257983-help-with-undefined-index/#findComment-1322361 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.