edit uploaded image with using class.upload.php

213 Views Asked by At

I made simple upload system with using class.upload.php and it works great while adding new into database. But i have problem when i need to edit my entry. While editing entry i don't want to edit image but it sent it blank, also if i select image again it sent it blank too. Here is my code.

Can explain my problem.

<?php require_once("conn.php");
require_once ("class.upload.php");

$catid = $_POST['catid'];
$title = $_POST['title'];
$descc = $_POST['descc'];
$keyw = $_POST['keyw'];
$message = $_POST['message'];
$Image = $_FILES['Image'];
$randnum = rand();
$foo = new upload($Image);
$filee = './Image'; 
 if ($foo->uploaded) { 
 $foo->image_resize = true;
 $foo->file_new_name_body = $randnum;
 $foo->image_x = 550;
 $foo->image_y = 440;
 $foo->process($filee);
 if ($foo->processed) { 
 echo 'Image uploaded.';
 echo $foo->file_dst_name;
 $foo->clean();
 } else {
 echo 'Error. : ' . $foo->error;
 }
 }
$Image7 = $foo->file_dst_name;
    if($_GET[pass] == 1)
    {   
        if(!isset($_POST[catid]) || empty($_POST[catid])){
            $hata = "Required area.";
        }
        if(!isset($_POST[title]) || empty($_POST[title])){
            $hata = "Required area.";
        }
        if(!isset($_POST[descc]) || empty($_POST[descc])){
            $hata = "Required area.";
        }       
        if(!isset($_POST[keyw]) || empty($_POST[keyw])){
            $hata = "Required area.";
        }
        if(!isset($_POST[message]) || empty($_POST[message])){
            $hata = "Required area.";
        }
        if(!$hata){
            mysql_query("UPDATE product SET
                catid='$_POST[catid]',
                title='$_POST[title]',
                descc='$_POST[descc]',
                keyw='$_POST[keyw]',
                message='$_POST[message]',
                Image='$_POST[Image]'
                WHERE id='$_POST[id]'
                "); 
            $mesaj = "OK!";
        }

    }
    $sonuc = mysql_query("select * from product WHERE id='$_GET[product]'");
    $edit = mysql_fetch_array($sonuc);
    $sonuc1 = mysql_query("select * from category");
    $edit1 = mysql_fetch_array($sonuc1);
?>
2

There are 2 best solutions below

1
On

try to change the update query

at Image='$_POST[Image]'

with Image='$Image7'

0
On

Fatih you can use a variable (i.e. $saved_image_name) instead of $POST[Image] at sql query. Set this variable to new name if uploaded else old value of db field.

...
...
$foo = new upload($Image);
$filee = './Image'; 

$saved_image_name = " Image "; // name of db field.

 if ($foo->uploaded) { 
 $foo->image_resize = true;
 $foo->file_new_name_body = $randnum;
 $foo->image_x = 550;
 $foo->image_y = 440;
 $foo->process($filee);
 if ($foo->processed) { 
 echo 'Image uploaded.';
 echo $foo->file_dst_name;
 // Note the quotes
 $saved_image_name = " '$foo->file_dst_name' ";
 $foo->clean();
 } else {
 echo 'Error. : ' . $foo->error;
 }
 }
// no use anymore $Image7 = $foo->file_dst_name;
...
...

if(!$hata){
            mysql_query("UPDATE product SET
                catid='$_POST[catid]',
                title='$_POST[title]',
                descc='$_POST[descc]',
                keyw='$_POST[keyw]',
                message='$_POST[message]',
                Image= $saved_image_name // note the quotes
                WHERE id='$_POST[id]'
                "); 

... ...