求助!如何将上传图片插入到数据库中。

#1 改变命运2023

我现在可已经将上传的图片分为四经缩略图,但是却无法获取这四张图的地址,并将此插入到数据库中。以下是我的代码:
控制器:
   import("include/uploadFile.php");
class ToolBox extends spController {
  //上传类测试
  function upFile(){
      $tpl = $this->spArgs("tpl","default");
       $images = spClass("s_images");
       $postres = $this->spArgs("id");
         $condition = array('id'=>$postres);
        
   $this->title='文件上传';
   if($this->spArgs('up')){
    $upFlie=new uploadFile();
    $uploadImg=new uploadImg();
    $result=$upFlie->upload_file($_FILES['image']);
    $Img1=$uploadImg->img_resized(360,360,"$upFlie->uploaded");
    $Img2=$uploadImg->img_resized(196,196,"$upFlie->uploaded");
    $Img3=$uploadImg->img_resized(44,44,"$upFlie->uploaded");
    $msg=$upFlie->errmsg;
     $updaterow=array('image' => $this->spArgs('image'));
         //$images->update($condition,$updaterow);
         print_r($updaterow);exit;
    if(($result)){
     $this->success=$msg;
        
    }else {
     $this->errmsg=$msg;
         
    }
   
   }
     $this->display("{$tpl}/manage/upFile.html");
  }

}
模板:
?>

<{$title}>


<{if $success}>

<{$success}>  继续上传返回项目首页


<{elseif $errmsg}>

<{$errmsg}>  重新上传


<{else}>

   

        
        
        
   


<{/if}>


2012-04-09 14:44:15

#2 jake

{:soso_e127:},你可以把上传图片的URL地址存到数据库,图片本身一般不建议存数据库。

看看php手册关于文件上传部分的教程,然后找到图片URL即可。http://cn.php.net/manual/zh/features.file-upload.php

2012-04-09 22:09:46

#3 改变命运2023

jake 发表于 2012-4-9 22:09
,你可以把上传图片的URL地址存到数据库,图片本身一般不建议存数据库。

看看php手册关于文 ...
呵呵,谢谢jake,我现在已经获取图片地址,但是却出现了另一个问题,无法获得id,因此无法将我所得的几张缩略图的地址插入到数据库中。郁闷!您能帮帮我吗?
image.html
上传图片
---------------------------------------------------------------------------------------------------------------
Toolbox.php
$images = spClass("s_images");
          $postres = $this->spArgs("img_id");
         $condition = array('img_id'=>$postres);
     $updaterow=array('image' =>$Image0,
         'b_image'=>$Image1,
          'm_image'=>$Image2,
          's_image'=>$Image3);
   $this->title='文件上传';
-------------------------------------------------------------------------
upFile.html

<{$title}>


<{if $success}>

<{$success}>  继续上传返回项目首页


<{elseif $errmsg}>

<{$errmsg}>  重新上传


<{else}>

   

        
        
        
   


<{/if}>

2012-04-11 10:53:53

#4 jake

不明白什么叫无法获得ID,你把ID放到一个input里面,用spArgs就可以拿到。或者可以参考入门教程。

2012-04-11 12:56:37

#5 改变命运2023

jake 发表于 2012-4-11 12:56
不明白什么叫无法获得ID,你把ID放到一个input里面,用spArgs就可以拿到。或者可以参考入门教程。 ...
{:soso_e113:} ,已经解决了,也就是说,我现在往数据库插入的id已经获取了,要插入的图片地址也得到了,但是还是无法更新数据库记录。后来我用"print_r($postres)",测试出来的结果为空,就是无法得到id。也就是说在if条件语句内得不到.但是在if条件以为可以得到 id值。所以无法更新数据库表记录。请问这该怎么办呢?我试着将$posttres设为全局变量,也不行。以下是我的代码:
function upFile(){
    $tpl = $this->spArgs("tpl","default");
         $images = spClass("s_images");
$postres = $this->spArgs("img_id");
         $condition = array('img_id'=>$postres);

   
   $this->title='文件上传';
   if($this->spArgs('up')){
    $upFlie=new uploadFile();
    $uploadImg=new uploadImg();
    $result=$upFlie->upload_file($_FILES['image']);
    $Img1=$uploadImg->img_resized(360,360,"$upFlie->uploaded");
    $Img2=$uploadImg->img_resized(196,196,"$upFlie->uploaded");
    $Img3=$uploadImg->img_resized(44,44,"$upFlie->uploaded");
    $Image0=$upFlie->uploaded;
    $Image1=$upFlie->uploaded1;
    $Image2=$upFlie->uploaded2;
    $Image3=$upFlie->uploaded3;
    $msg=$upFlie->errmsg;
     
     $updaterow=array('image' =>$Image0,
         'b_image'=>$Image1,
          'm_image'=>$Image2,
          's_image'=>$Image3);
    print($postres);exit;
    $images->update($condition,$updaterow);
         //$images->update($condition,$updaterow);
        
    if(($result)){
     $this->success=$msg;
        
    }else {
     $this->errmsg=$msg;
         
    }
   
   }
     $this->display("{$tpl}/manage/upFile.html");
  }

}
?>



补充内容 (2012-4-11 14:22):
我也是刚学框架,所以许多问题可能有些菜,还词望能理解

2012-04-11 14:20:39

#6 jake

function upFile(){
后面加上
dump($this->spArgs());

看看提交上来的全部东西。

估计不是img_id获取不到,因为如果是获取不到,用它作为condition来update的话,会出错的。估计是up这个参数不存在。

2012-04-11 18:24:55

#7 改变命运2023

jake 发表于 2012-4-11 18:24
function upFile(){
后面加上
dump($this->spArgs());
Array(    [c] => ToolBox    [a] => upFile    [img_id] => 23    [PHPSESSID] => d12dddbfcf3e783e423f16ab50fbe5cf)
能获取到
这个是upFile.html,有"up"啊

<{$title}>


<{if $success}>

<{$success}>  继续上传返回项目首页


<{elseif $errmsg}>

<{$errmsg}>  重新上传


<{else}>

       

               
               
               
       


<{/if}>




补充内容 (2012-4-11 18:59):
Array
(
    [c] => ToolBox
    [a] => upFile
    [img_id] =>
    [up] => 上传
    [PHPSESSID] => d12dddbfcf3e783e423f16ab50fbe5cf
)


文件上传

文件./upload/20120411105955.jpg上传成功!

2012-04-11 18:58:48

#8 jake

当up有值的时候,img_id就没有值。当up没有值的时候,img_id就有值。

好好看看你的表单吧。提交的值就能说明一切,是表单出了问题(这是因果关系)。估计是$one.img_id的时候没有取得id吧。

2012-04-11 22:53:04

#9 改变命运2023

jake 发表于 2012-4-11 22:53
当up有值的时候,img_id就没有值。当up没有值的时候,img_id就有值。

好好看看你的表单吧。提交的值就能说 ...
是啊,就是没有取得到id呵呵。但是为什么获取不了呢,我也郁闷了。我再找找看吧非常感谢jake能这么耐心的帮我解答困惑

2012-04-13 08:45:27

#10 改变命运2023

jake 发表于 2012-4-11 22:53
当up有值的时候,img_id就没有值。当up没有值的时候,img_id就有值。

好好看看你的表单吧。提交的值就能说 ...
解决了,就是表单的问题,后来,我在控制器加了一个查询语句,然后又 在表单里加了一个标签,并隐藏,最后得出了这个值。呵呵,谢谢 jake

2012-04-13 18:13:55