Jump to content

problem with second else statement (needed for updating database)


AdRock

Recommended Posts

I am trying to update the database but have come across a problem.  The problem lies with the second else statement but how do i fix it?

I have form where I can make changes to the record and one of the fields is to upload an image.  Sometimes I don't want to upload an image and keep the one that's already in the database.  This is where the error occurs.

Before, if i didn't select an image it would update the image name as blank so when called no image would be displayed.

What i want is if i don't upload an image the imge name stays the same.

Here is the code i tried but doesn't work
[code]include_once("../includes/connection.php");

   if(empty($pic)) {
       $query="UPDATE news SET title='$name', content='$message' WHERE id='$ud_id'";
       mysql_query($query);
       }
   else
       {
       $query="UPDATE news SET title='$name', content='$message', photo='$pic' WHERE id='$ud_id'";
       mysql_query($query);

       //Writes the photo to the server
       if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
       {

       //Tells you if its all ok
       echo "The file ".basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
       }
    }
   else {

       //Gives and error if its not
       echo "Sorry, there was a problem uploading your file.";
       }

   mysql_close();[/code]
Link to comment
Share on other sites

Why don't you just not update that part on the second else, like so:

[code]

include_once("../includes/connection.php");

    if(empty($pic)) {
        $query="UPDATE news SET title='$name', content='$message' WHERE id='$ud_id'";
        mysql_query($query);
        }
    elseif
        {

        $query="UPDATE news SET title='$name', content='$message', WHERE id='$ud_id'";
        mysql_query($query);

        //Writes the photo to the server
        if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
        {

        //Tells you if its all ok
        echo "The file ".basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
        }
    }
    else {

        //Gives and error if its not
        echo "Sorry, there was a problem uploading your file.";
        }

    mysql_close();
[/code]

and as AndyB said. I added that to this code.
Link to comment
Share on other sites

i tried that but it didn't work

I used this which now does work

[code]    include_once("../includes/connection.php");

    if(empty($pic)) {
        $query="UPDATE news SET title='$name', content='$message' WHERE id='$ud_id'";
        mysql_query($query);
        }
    else
        {
        $query="UPDATE news SET title='$name', content='$message', photo='$pic' WHERE id='$ud_id'";
        mysql_query($query);

        //Writes the photo to the server
        if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
        {

        //Tells you if its all ok
        echo "The file ".basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
        }

        //Gives and error if its not
        echo "Sorry, there was a problem uploading your file.";
        }

    mysql_close();[/code]
Link to comment
Share on other sites

I do have a similar problem now though.

Not only sometimes do I not what to update the image but I also don't want to change a date that's in the database

Where i have this [b]list($day, $month, $year) = explode("-", $event);[/b] when i don't change the date, it goes crazy and makes a weird date in thge future and i know why.....i just need to be able to ignore either the image, the date or both

[code]    include_once("../includes/connection.php");
    mysql_connect($host,$user,$password);
    @mysql_select_db($database) or die( "Unable to select database");

    list($day, $month, $year) = explode("-", $event);
    $EventDate = date('Y-m-d', strtotime("$year/$month/$day"));

    $query="UPDATE events SET title='$name', eventdate='$EventDate', content='$message', photo='$pic' WHERE id='$ud_id'";
    mysql_query($query);

    //Writes the photo to the server
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
    {

    //Tells you if its all ok
    echo "The file ".basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
    }
    else {

    //Gives and error if its not
    echo "Sorry, there was a problem uploading your file.";
    }

    mysql_close();[/code]
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.