Jump to content

Don't display image if not in DB


sean14592

Recommended Posts

Hi, I have stored my image name in db after uploading it, so this is all well for the first 2 images as they are complusary. Though for example image 8 that is not required. How do I stop it from being shown if the database field is empty?

<?php

$connect = mysql_connect($host,$username,$password) or die("Error connecting to Database!". mysql_error());
mysql_select_db($database,$connect) or die("cannot select database!".mysql_error());


//photo8
$check8 = mysql_query("select photo8url from properties where id='$id'") or die(mysql_error());
$result8 = mysql_query($check8);
if (mysql_num_rows($check8) > 0) {
     $a_record = mysql_fetch_array($check8);
     $photo8url = $a_record['photo8url'];
 $photo8url = preg_replace('/\s+/', '%20', $photo8url);

} ?>

<?php  

			if ($result8 = ""){}
			else{
			echo ("<img src=". $id ."/images/".$photo8url." width=\"235\" border=\"1\">");
			}	?>

Link to comment
Share on other sites

SQL Injections could easily be done with your script! Check if the id is numeric otherwise DON'T execute this page.

 

To your quest:

<?php
   $res = mysql_query('YOUR QUERY');
   if (mysql_num_rows($res)) {
      //View image
   }else {
      echo 'Image not found';
   }
?>

Link to comment
Share on other sites

Hi, I have tried the following:

 

$res = mysql_query("select photo8url from properties where id='$id' AND photo8url =' ' ") or die(mysql_error());
if (mysql_num_rows($res)) {
echo ("<img src=". $id ."/images/".$photo8url." width=\"235\" border=\"1\">");
}else {
echo 'Image not found';
}

 

 

 

Though its trying to show a image when the data base is empty.

 

Cheers

Sean Preston

Link to comment
Share on other sites

I have also tried....

 


$connect = mysql_connect($host,$username,$password) or die("Error connecting to Database!". mysql_error());
mysql_select_db($database,$connect) or die("cannot select database!".mysql_error());

//photo1
$check1 = mysql_query("select photo1url from properties where id='$id'") or die(mysql_error());
$result1 = mysql_query($check1);
if (mysql_num_rows($check1) > 0) {
$a_record = mysql_fetch_array($check1);
$photo1url = $a_record['photo1url'];
$photo1url = preg_replace('/\s+/', '%20', $photo1url);
}
else {
$photo1_check = "1";
}




if ($photo1_check = "1") {
echo ("no image!!!!!!");
}else {
echo ("<img src=". $id ."/images/".$photo1url." width=\"235\" border=\"1\">");
}
?>


 

 

This shows "no image" even if a image has been stored,lol

 

This is driving me crazyyyy!

 

Sean Preston

Link to comment
Share on other sites

I have figued out (so I think) that somet is wrong here

 

$check8 = mysql_query("select photo8url from properties where id='$id'") or die(mysql_error());
if (mysql_num_rows($check8) > 0) {
     $a_record = mysql_fetch_array($check8);
     $photo8url = $a_record['photo8url'];
 $photo8url = preg_replace('/\s+/', '%20', $photo8url);
 }
else {
$photo8_check = "1";
}

 

because if I....

 

echo $photo8_check;

 

I should see the number '1', as in the database field Photo8url using the id is completely empty.

 

Do you understand what I mean?

 

Cheers

Sean

Link to comment
Share on other sites

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.