Jump to content


Photo

problem with second else statement (needed for updating database)


  • Please log in to reply
4 replies to this topic

#1 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 26 August 2006 - 10:52 PM

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
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();

If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 26 August 2006 - 10:57 PM

if
elseif
else
Legend has it that reading the manual never killed anyone.
My site

#3 pocobueno1388

pocobueno1388
  • Members
  • PipPipPip
  • Advanced Member
  • 3,369 posts
  • LocationPalm Beach, Florida

Posted 26 August 2006 - 10:58 PM

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


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();

and as AndyB said. I added that to this code.

#4 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 26 August 2006 - 11:16 PM

i tried that but it didn't work

I used this which now does work

    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();

If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#5 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 26 August 2006 - 11:20 PM

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 list($day, $month, $year) = explode("-", $event); 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

    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();

If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users