Jump to content

if there or not


SkyRanger

Recommended Posts

I am having a problem.

 

$pic = image.jpg

pic2 =

pic3 =

 

pic2 and pic3 there is nothing in database

 

$query = "SELECT * FROM `cinvent` WHERE `oname`='$loggedin' and iid='$iid'";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
while($row = $result->fetch_assoc()){

if ($row['pic'] == 0 ) {
echo "No Image";
} else {
$file1 = $row['pic'];
$delfile = "members/".$loggedin."/inventory/".$file1;
unlink($delfile);
}
if ($row['pic2'] == 0 ) {
echo "No Image 2";
} else {
$file2 = $row['pic2'];
$delfile2 = "members/".$loggedin."/inventory/".$file2;
unlink($delfile2);
}
if ($row['pic3'] == 0 ) {
echo "No Image 3";
} else {
$file3 = $row['pic3'];
$delfile3 = "members/".$loggedin."/inventory/".$file3;
unlink($delfile3);
}

 

Even though there is nothing in the database it is still trying to run the unlink for pic2 and pic3

 

Not sure what the problem is.

 

It will remove the image from 'pic'

Link to comment
https://forums.phpfreaks.com/topic/273570-if-there-or-not/
Share on other sites

You are checking to see if the database column is an integer value of zero

if ($row['pic2'] == 0 ) {

 

then you are using the value of the database column as a file name string

$file2 = $row['pic2'];

 

You should probably just test to see if the column is empty:

if (empty($row['pic2'](( {

Link to comment
https://forums.phpfreaks.com/topic/273570-if-there-or-not/#findComment-1407852
Share on other sites

Perfect, thanks DavidAM.

 

New Code that works:

 

$query = "SELECT  * FROM `cinvent` WHERE `oname`='$loggedin' and iid='$iid'";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
while($row = $result->fetch_assoc()){

       if (empty($row['pic'])) {
       echo "No Image";
       } else {
        $file1 = $row['pic'];
        $delfile = "members/".$loggedin."/inventory/".$file1;
        unlink($delfile);
       }
       if (empty($row['pic2'])) {
       echo "No Image 2";
       } else {
        $file2 = $row['pic2'];
        $delfile2 = "members/".$loggedin."/inventory/".$file2;
        unlink($delfile2);
       }
       if (empty($row['pic3'])) {
       echo "No Image 3";
       } else {
        $file3 = $row['pic3'];
        $delfile3 = "members/".$loggedin."/inventory/".$file3;
        unlink($delfile3);
       }
}

Link to comment
https://forums.phpfreaks.com/topic/273570-if-there-or-not/#findComment-1407854
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.