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:

<?php
$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";
?>

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

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

Sure, no if!

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.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.