【问题标题】:edit uploaded image with using class.upload.php使用 class.upload.php 编辑上传的图像
【发布时间】:2015-09-13 16:42:30
【问题描述】:

我使用class.upload.php 制作了简单的上传系统,它在向数据库添加新内容时效果很好。但是当我需要编辑我的条目时我遇到了问题。在编辑条目时我不想编辑图像但它发送它为空白,如果我再次选择图像它也发送它为空白。这是我的代码。

可以解释我的问题。

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

【问题讨论】:

  • 很简单,只需从更新查询中删除 Image='$_POST[Image]' 即可。
  • 如果我删除了我无法编辑的图像,谢谢你的回答:)
  • 在这种情况下,数据库需要图像字段。您需要将其从 NOT NULL 更改为 NULL。

标签: php mysql image file edit


【解决方案1】:

尝试更改更新查询

在 Image='$_POST[Image]'

with Image='$Image7'

【讨论】:

  • 您好,谢谢您的回答。我试过了,如果我在编辑时再次选择图像很好,但如果我不选择图像并编辑其他部分图像会损坏。如果我不选择任何图像,我想要它不会更改图像列。我希望我能解释一下
【解决方案2】:

您可以在 sql 查询中使用变量(即 $saved_image_name)而不是 $POST[Image]。如果已上传,则将此变量设置为新名称,否则为 db 字段的旧值。

...
...
$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]'
                "); 

... ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-08
    • 2018-01-10
    • 2019-10-07
    • 2019-07-27
    • 2023-03-14
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多