Jump to content

Archived

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

AdRock

problem with second else statement (needed for updating database)

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]

Share this post


Link to post
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.

Share this post


Link to post
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]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.