I am trying to write an if statement for image in my database. Example: if no image is relating to the recipeid do not post a blank image box leave blank as if there is nothing there..only show image for recipeid that has a picture.

This is my code that I have but I can not figure how to write the if code:

$con = mysql_connect("localhost", "test", "test") or die('Could not connect to server');
mysql_select_db("recipe", $con) or die('Could not connect to database');

$recipeid = $_GET['id'];

$query = "SELECT title,poster,shortdesc,ingredients,directions, calories, picture from recipes where recipeid = $recipeid";

$result = mysql_query($query) or die('Could not find recipe');
$row = mysql_fetch_array($result, MYSQL_ASSOC) or die('No records retrieved');
$title = $row['title'];
$poster = $row['poster'];
$shortdesc = $row['shortdesc'];
$ingredients = $row['ingredients'];
$directions = $row['directions'];
$calories = $row['calories'];
$picture = $row['thumbnail'];

$ingredients = nl2br($ingredients);
$directions = nl2br($directions);

echo "<h2>$title</h2>\n";

echo "by $poster <br><br>\n";
echo $shortdesc . "<br><br>\n";
echo "<h3>Ingredients:</h3>\n";
echo $ingredients . "<br><br>\n";

echo "<h3>Directions:</h3>\n";
echo $directions . "\n";
echo "<h3>Calories:</h3>\n";
echo $calories . "<br><br>\n";
echo "<img src=\"showimage.php?id=$recipeid\" border=\"0\" ></a>\n";

echo "<br><br>\n";

Edited by mike_2000_17: Fixed formatting

7 Years
Discussion Span
Last Post by vanessia_1999

The 'or die " exits from the code when the condition is met.
I don't see an If statement there at all.


Perhaps something like this:

if(isset($picture) && !empty($picture))
    echo "<img src=\"showimage.php?id=$recipeid\" border=\"0\"></a>\n";
else { /* code for replacement of image */ }

thanks Jerail, I figured it out

if($row['picture']) {
echo "<img src=\"showimage.php?id=$recipeid\" border=\"0\" ></a>\n";

I tried yours too to see if it would of also work but it removed the images/pictures that was posted. The code above works. But I gave you the solve point since you tried to help.


Edited by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.