Run different code if a field is blank

Hello there!
I need some help on a script. I have a form with that edits a row in the database, a form that have a browse button for images. What i need is to run a code if the field LajmeImg is not blank:

$sql = "UPDATE lajme SET KatLajmeID = '$KatLajmeID', LajmeEmri = '$LajmeEmri', LajmeLink = '$LajmeLink', LajmeIntro = '$LajmeIntro', LajmeTxt = '$LajmeTxt', LajmeVideo = '$LajmeVideo', LajmeTag = '$LajmeTag', LajmeImg = '$LajmeImg', LajmeAktiv = '$LajmeAktiv' WHERE LajmeID = '$LajmeID'";

…and, if it is blank, to run this statement without LajmeImg = ‘$LajmeImg’:

$sql = "UPDATE lajme SET KatLajmeID = '$KatLajmeID', LajmeEmri = '$LajmeEmri', LajmeLink = '$LajmeLink', LajmeIntro = '$LajmeIntro', LajmeTxt = '$LajmeTxt', LajmeVideo = '$LajmeVideo', LajmeTag = '$LajmeTag, LajmeAktiv = '$LajmeAktiv' WHERE LajmeID = '$LajmeID'";

And, this is the hole code of the edit page where i need to use that if statement:

<?php
include('header.php');
include ('includes/uploader.class.php');
    $uploader = new uploader();
    // Setting properties then Uploading the image
    $uploader->destDir = "D:/Program Files/VertrigoServ/www/oldi/imazhet/artikuj/";
    $uploader->upload($_FILES['LajmeImg']);
    
include('lart.php');
if(!$_GET['lajmeid'] && !isset($_POST['do_edit']) && !isset($_POST['edit_lajme'])) {

echo $error;

// Shfaq listen e lajmeve
lajmet_lista();

}



// Has the edit form been submitted?

if(isset($_POST['do_edit'])) {
    
    $LajmeID = $_POST['LajmeID'];
    $LajmeEmri = $_POST['LajmeEmri'];
    $KatLajmeID = $_POST['KatLajmeID']; // Kategoria Lajmeve
    $LajmeIntro = $_POST['LajmeIntro'];
    $LajmeTxt = $_POST['LajmeTxt'];
    $LajmeAktiv = $_POST['LajmeAktiv'];
    $LajmeTag = $_POST['LajmeTag'];
    $LajmeVideo = $_POST['LajmeVideo'];
    $LajmeLink = $_POST['LajmeLink'];
    //$LajmeImg = $_FILES['LajmeImg']['name']; // Me autoName = False; regjistron ne db emrin real te fotografise
    $LajmeImg = $uploader->source['name']; // Me autoName = true; regjistron ne db emrin random te fotografise i gjeneruar ne base64
    $LajmeImg = $_POST['LajmeImg'];
    
    $fshij = $_POST['fshij'];

    
    // Ticked the 'delete category' box? If so, delete and echo message.    
    if($fshij == 'fshij_LajmeID' && $error == '') {
        
    $sql =  "DELETE FROM lajme WHERE  LajmeID=$LajmeID LIMIT 1";
    $query = mysql_query($sql) or die("Fatal error: ".mysql_error());

    echo "<h3>U Fshi</h3>";
    echo "<div class='success_message'>Lajme <b>$LajmeID</b> u fshi nga sistemi.</div>";
    
    echo "<h2>What to do now?</h2><br />";
    echo "Go to the <a href='lajme_edit.php'>edit lajme</a> page.</li>";
        
    } else {
    
    // Validate the submitted information
    
        if(trim($LajmeID) == '0') {
            $error = '<div class="error_message">LajmeID - Attention! You cannot edit the main Administrator, use database.</div>';
        } else if(trim($LajmeEmri) == '') {
            $error = '<div class="error_message">LajmeEmri - Attention! You must enter a first name.</div>';
        } else if(trim($LajmeLink) == '') {
            $error = '<div class="error_message">LajmeUrl - Attention! You must enter a last name.</div>';
        } 
        
    // Password been entered? If so, validate and update information.
    
        if(trim($KatLajmeID) == '') {
        $error = '<div class="error_message">Attention! Nuk ke zgjedhur kategorine.</div>';
    } else if(trim($LajmeIntro) == '') {
        $error = '<div class="error_message">Attention! Nuk ke vene Intro.</div>';
    } else if(trim($LajmeEmri) == '') {
        $error = '<div class="error_message">Attention! Nuk ke vene emrin.</div>';
    } 
            
            if($error == '') {
        
            $sql = "UPDATE lajme SET KatLajmeID = '$KatLajmeID', LajmeEmri = '$LajmeEmri', LajmeLink = '$LajmeLink', LajmeIntro = '$LajmeIntro', LajmeTxt = '$LajmeTxt', LajmeVideo = '$LajmeVideo', LajmeTag = '$LajmeTag', LajmeImg = '$LajmeImg', LajmeAktiv = '$LajmeAktiv' WHERE LajmeID = '$LajmeID'";
            $query = mysql_query($sql) or die("Fatal error: ".mysql_error());
            
            // $sql2 = "SELECT * FROM lajme_kategori WHERE LKatAktiv = PO"; 
            //$sql2 = "SELECT B.LKatEmri, A.LajmeID AS LajmeID, A.LajmeEmri AS LajmeEmri, A.LajmeLink AS LajmeLink, A.LajmeIntro AS LajmeIntro, A.LajmeTxt AS LajmeTxt, A.LajmeTag AS LajmeTag,  A.LajmeVideo AS LajmeVideo, A.LajmeAktiv AS LajmeAktiv, B.LKatAktiv = LKatAktiv FROM lajme A LEFT JOIN lajme_kategori B ON A.KatLajmeID = B.LKatID WHERE B.LKatID = KatLajmeID AND LKatAktiv = PO AND LajmeAktiv = PO ";
            //$sql2 = "SELECT * FROM lajme_Kategori WHERE LKatID = '$KatLajmeID'";
            //$result2 = mysql_query($sql2);
        
            echo "<h2>U Modifikua</h2>";
            echo "<div class='success_message'>Lajmi u modifikua: <a href='lajme_edit.php?lajmeid=$LajmeID'><b>$LajmeEmri</b></a> me ID <b>$LajmeID </b>.</div>";
            
            echo "<h2>C'deshironi te beni tani?</h2><br />";
            echo "Vizitoni <a href='lajme_edit.php'>listen e lajmeve</a>.</li>";
            
            }
    
    
        }
        
    }


// Has a category been selected to edit?

if($_GET['lajmeid'] && !isset($_POST['do_edit']) && !isset($_POST['edit_lajme']) || $error != '') {

    $LajmeID = $_GET['lajmeid'];
    
    $sql = "SELECT B.LKatEmri, A.LajmeID AS LajmeID, A.LajmeEmri AS LajmeEmri, A.LajmeLink AS LajmeLink, A.LajmeIntro AS LajmeIntro, A.LajmeTxt AS LajmeTxt, A.LajmeTag AS LajmeTag, A.LajmeImg AS LajmeImg, A.LajmeVideo AS LajmeVideo, A.LajmeAktiv AS LajmeAktiv, A.KatLajmeID AS KatLajmeID FROM lajme A LEFT JOIN lajme_kategori B ON A.KatLajmeID = B.LKatID WHERE B.LKatID = KatLajmeID AND A.LajmeID='$LajmeID'"; 
    $result = mysql_query($sql);
    
    $row = mysql_fetch_array($result);
    
    $LajmeEmri = $row['LajmeEmri'];
    $LajmeLink = $row['LajmeLink'];
    $LajmeIntro = $row['LajmeIntro'];
    $LajmeTxt = $row['LajmeTxt'];
    $LajmeVideo = $row['LajmeVideo'];
    $LajmeTag = $row['LajmeTag'];
    $LajmeImg = $row['LajmeImg'];
    $LajmeAktiv = $row['LajmeAktiv'];
    $KatLajmeID = $row['KatLajmeID'];
    $LKatEmri = $row['LKatEmri'];
    
        
    echo $error;
            
    echo "<h2>Informacionet per lajmin ( ".stripslashes($row['LajmeEmri'])." )</h2>";
        
?>

<form action="" method="post" enctype="multipart/form-data" />
<input type="hidden" name="LajmeID" value="<?=$row['LajmeID'];?>" />

<hr /><label>Kategoria</label>
<select name="KatLajmeID">
<option selected value="<?=stripslashes($row['KatLajmeID']);?>"><?=$LKatEmri ?></option> <?php // Marrja e te dhenave ekzistente ?>
<?php
$sql2 = "SELECT * FROM lajme_kategori WHERE LKatAktiv = 'PO'"; // Query per te marre kategorite nga database
            $result2 = mysql_query($sql2);
?>
<?
while($kategoria = mysql_fetch_array($result2)) {
    echo '<option value="'.stripslashes($kategoria['LKatID']).'">'.stripslashes($kategoria['LKatEmri']).'</option>'; // Lista e kategorive
}
?>
</select>

<hr /><label>Titulli</label>
<input type="text" name="LajmeEmri" value="<?=stripslashes($row['LajmeEmri']);?>" class="lf" /><br />

<hr /><label>Linku</label>
<input type="text" name="LajmeLink" value="<?=stripslashes($row['LajmeLink']);?>" class="lf" /><br />



<hr /><label>Intro</label><textarea id="LajmeIntro" name="LajmeIntro" /><?=stripslashes($row['LajmeIntro']);?></textarea>
<script type="text/javascript">
    CKEDITOR.replace( 'LajmeIntro',
    {
        filebrowserBrowseUrl : 'includes/ckfinder/ckfinder.html',
        filebrowserImageBrowseUrl : 'includes/ckfinder/ckfinder.html?Type=Images',
        filebrowserFlashBrowseUrl : 'includes/ckfinder/ckfinder.html?Type=Flash',
        filebrowserUploadUrl : 'includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
        filebrowserImageUploadUrl : 'includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
        filebrowserFlashUploadUrl : 'includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash',
    });
</script><br />



<hr /><label>Lajmi</label>
<textarea id="LajmeTxt" name="LajmeTxt" /><?=stripslashes($row['LajmeTxt']);?></textarea>
<script type="text/javascript">
    CKEDITOR.replace( 'LajmeTxt',
    {
        filebrowserBrowseUrl : 'includes/ckfinder/ckfinder.html',
        filebrowserImageBrowseUrl : 'includes/ckfinder/ckfinder.html?Type=Images',
        filebrowserFlashBrowseUrl : 'includes/ckfinder/ckfinder.html?Type=Flash',
        filebrowserUploadUrl : 'includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
        filebrowserImageUploadUrl : 'includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
        filebrowserFlashUploadUrl : 'includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash'
    });
</script><br /> 


<hr /><label>Video</label>
<textarea id="LajmeVideo" name="LajmeVideo" rows="6" wrap="soft" style="width:100%;" /><?=stripslashes($row['LajmeVideo']);?></textarea><br />


<hr />Fotografia: <img src="../imazhet/artikuj/<?=stripslashes($row['LajmeImg']);?>" width="150" align="middle" /><br /><br />
<label>Fotografia</label> <input type="file" size="60" name="LajmeImg" id="LajmeImg" class="lf" /><br />

<hr /><label>Tag</label>
<input type="text" name="LajmeTag" value="<?=stripslashes($row['LajmeTag']);?>" class="lf" /><br />

<hr /><label style="width: 50%;">Aktive</label>
<input id="LajmeAktiv" name="LajmeAktiv" type="checkbox" <?php if ($LajmeAktiv == "PO") { echo "checked";} ?> value="PO">
<br /><br />
<hr /><div class="error_message">Do fshish kete lajm? (Nuk kthehet mbrapsh!) <input type="checkbox" class="checkbox" name="fshij" value="fshij_LajmeID"></div>
<hr /><input type="submit" value="Ruaj Ndryshimet" name="do_edit" class="button" />
</form>

<? } 
include('poshte.php'); ?> 

Can someone help me on this, please!

You are assigning blank to $LajmeImg!

It should be

if(($LajmeImg == '')....

I have used this statement too but seems that variable $LajmeImg didn’t get informations from the uploader.class and didn’t have any information to update the db :frowning:

if(empty($_FILES['LajmeImg']) && ($error == '')) {
        
            $sql = "UPDATE lajme SET KatLajmeID = '$KatLajmeID', LajmeEmri = '$LajmeEmri', LajmeLink = '$LajmeLink', LajmeIntro = '$LajmeIntro', LajmeTxt = '$LajmeTxt', LajmeVideo = '$LajmeVideo', LajmeTag = '$LajmeTag', LajmeAktiv = '$LajmeAktiv' WHERE LajmeID = '$LajmeID'";
            $query = mysql_query($sql) or die("Fatal error: ".mysql_error());
            }
            else if (!empty($_FILES['LajmeImg']) && ($error == '')) {
            $sql = "UPDATE lajme SET KatLajmeID = '$KatLajmeID', LajmeEmri = '$LajmeEmri', LajmeLink = '$LajmeLink', LajmeIntro = '$LajmeIntro', LajmeTxt = '$LajmeTxt', LajmeVideo = '$LajmeVideo', LajmeTag = '$LajmeTag', LajmeImg = '$LajmeImg', LajmeAktiv = '$LajmeAktiv' WHERE LajmeID = '$LajmeID'";
            $query = mysql_query($sql) or die("Fatal error: ".mysql_error());  
// if the field LajmeImg is not blank:
if ($LajmeImg != '') { 
    do this;
} else {
    do that;
}

Yep, i know that i should use an if, but i don’t know how :frowning:

Use an ‘if’

I like that! :inspector:
Well done, captain :slight_smile:

Thank you for reply. My statement looks like this now:

if(($LajmeImg = '') && ($error == '')) {
        
            $sql = "UPDATE lajme SET KatLajmeID = '$KatLajmeID', LajmeEmri = '$LajmeEmri', LajmeLink = '$LajmeLink', LajmeIntro = '$LajmeIntro', LajmeTxt = '$LajmeTxt', LajmeVideo = '$LajmeVideo', LajmeTag = '$LajmeTag', LajmeAktiv = '$LajmeAktiv' WHERE LajmeID = '$LajmeID'";
            $query = mysql_query($sql) or die("Fatal error: ".mysql_error());
            }
            else if (($LajmeImg != '') && ($error == '')) {
            $sql = "UPDATE lajme SET KatLajmeID = '$KatLajmeID', LajmeEmri = '$LajmeEmri', LajmeLink = '$LajmeLink', LajmeIntro = '$LajmeIntro', LajmeTxt = '$LajmeTxt', LajmeVideo = '$LajmeVideo', LajmeTag = '$LajmeTag', LajmeImg = '$LajmeImg', LajmeAktiv = '$LajmeAktiv' WHERE LajmeID = '$LajmeID'";
            $query = mysql_query($sql) or die("Fatal error: ".mysql_error());

But didn;t worked :frowning: Any suggestions pls?

guido2004: I know something about if statements but i’m not an expert on this. For that i need your help :wink: Thanks anyway!

Thanks to everyone for helping me.

I have used both ways:

if(empty($_FILES['LajmeImg']) && ($error == '')) {

            $sql = "UPDATE lajme SET KatLajmeID = '$KatLajmeID', LajmeEmri = '$LajmeEmri', LajmeLink = '$LajmeLink', LajmeIntro = '$LajmeIntro', LajmeTxt = '$LajmeTxt', LajmeVideo = '$LajmeVideo', LajmeTag = '$LajmeTag', LajmeAktiv = '$LajmeAktiv' WHERE LajmeID = '$LajmeID'";

            $query = mysql_query($sql) or die("Fatal error: ".mysql_error());

            }

            else if (!empty($_FILES['LajmeImg']) && ($error == '')) {

            $sql = "UPDATE lajme SET KatLajmeID = '$KatLajmeID', LajmeEmri = '$LajmeEmri', LajmeLink = '$LajmeLink', LajmeIntro = '$LajmeIntro', LajmeTxt = '$LajmeTxt', LajmeVideo = '$LajmeVideo', LajmeTag = '$LajmeTag', LajmeImg = '$LajmeImg', LajmeAktiv = '$LajmeAktiv' WHERE LajmeID = '$LajmeID'";

            $query = mysql_query($sql) or die("Fatal error: ".mysql_error()); 

if(($LajmeImg == '') && ($error == '')) {

        

            $sql = "UPDATE lajme SET KatLajmeID = '$KatLajmeID', LajmeEmri = '$LajmeEmri', LajmeLink = '$LajmeLink', LajmeIntro = '$LajmeIntro', LajmeTxt = '$LajmeTxt', LajmeVideo = '$LajmeVideo', LajmeTag = '$LajmeTag', LajmeAktiv = '$LajmeAktiv' WHERE LajmeID = '$LajmeID'";

            $query = mysql_query($sql) or die("Fatal error: ".mysql_error());

            }

            else if (($LajmeImg !== '') && ($error == '')) {

            $sql = "UPDATE lajme SET KatLajmeID = '$KatLajmeID', LajmeEmri = '$LajmeEmri', LajmeLink = '$LajmeLink', LajmeIntro = '$LajmeIntro', LajmeTxt = '$LajmeTxt', LajmeVideo = '$LajmeVideo', LajmeTag = '$LajmeTag', LajmeImg = '$LajmeImg', LajmeAktiv = '$LajmeAktiv' WHERE LajmeID = '$LajmeID'";

            $query = mysql_query($sql) or die("Fatal error: ".mysql_error()); 

but, in both of them, when the field is empty didn’t let the LajmeImg DB field out of the query but updates the database with a value that i don;t know where it gets that have only the name of the image existent on the DB without extension (ex: if the existent image name is imagename.jpg now it looks like imagename.). I supose that this mean that “if” statement didn;t worked correctly.
Hope that someone will help me on this. Thank you!

Thank you Guido, most kind of you!

I take this so seriously that I have created a couple of “clippings” for my text editor (BBEdit) to help me look at variables, arrays and objects while debugging. Since I’m now using more complex arrays in my work, I also use it to help me visualize the multidimensional arrays. For simple variables the “echo” clipping outputs:

echo "variableName= " . $variableName . "<br>\
";

For objects and arrays I use the print_r clipping

echo "arrayName= \
<pre>\
";
print_r ($arrayName);
echo "</pre>\
";

Since I use them again and again, they have become a huge time saver. :cool:

Kleidi, debugging is an art and the best teacher is Sherlock Holmes. His passion was to find the evidence and then apply his keen logic to it. Just as in solving criminal mysteries, to debug you have to know what the various variables contain at any point in the process. If an if clause fails, you have to look at the variable to find out why it fails. Most often it is because the data that you expect to be there is not there or is something else.

In your case the first thing you should look at is what $POST returns with

print_r $_POST;

If the $POST variable is as it should be, then you keep going along the code testing it at each point until you find where it goes wrong. Just like Sherlock Holmes did, check everything with a magnifying glass. :wink:

Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth.

Sir Arthur Conan Doyle in “The Adventure of the Beryl Coronet”, spoken by the character Sherlock Holmes.

I have tried this way to but the file is uploaded and it’s path is not updated on the db :frowning:

In that case, I’d say that you should start studying some very basic php tutorials. :slight_smile: