更新数据库中的多个图像

时间:2013-09-30 11:33:21

标签: javascript php

先谢谢

我在更新数据库中的多个图像时遇到问题。所以我需要帮助...... 我在做的是: 在更新的情况下,我正在检索函数中的图像数据并在设计页面中调用该函数。该功能包括从数据中获取图像的代码,能够在更新页面中显示图像,但如果删除任何一个图像,则无法更新图像,然后更新数据,以便先前的图像在数据库中更新,意味着说以前的图像是以$ _POST [“AircraftImage”]中的数组形式出现的 这是下面的代码: -

public function getimageswithaircraftid ($AircarftID){
    try{
        // Query to retrieve Aircraft Images from database...
        $query = mysql_query ("SELECT * FROM `aircraft_image` WHERE      `Aircraft_ID` = '".$AircarftID."' ");
    if (mysql_num_rows ($query) > 0){
        $tabledata = '';
        //$_POST["AircraftImage"] = array();
        $tabledata = "<th>File Name</th><th></th><th>File Size</th><th>Image Preview</th><th>Delete</th>";
        while ($execute = mysql_fetch_object ($query)){
            $filepath = $execute->uploadpath;
            $split    = explode('/', $filepath);

            $tabledata .= "<tr class=record><td>".$split[5]."</td>";
            $tabledata .= "<td><input type=text name=AircraftImage[] value=".$split[5]."::".$execute->filesize. "></td>";
            $tabledata .= "<td>".$execute->filesize."</td>";
            $tabledata .= "<td><a target=_blank href=".$execute->uploadpath."><img src=".$execute->uploadpath." height=70 width=70 /></a></td>";
            $tabledata .= "<td><a href=# id=".$execute->AircraftImageId." class=delbutton>
            <img title=Delete src=images/delete.png ></a></td></tr>";
            //$tabledata .= "<input type=text name=AircraftImage[] value=".$split[5]."::".$execute->filesize. ">";
        }
        print $tabledata;   
    }
    }
    catch(Exception $ex){

    }   
}

这是插入/更新图像的代码..

public function uploadimage ($AircraftImage = array()){
    if (!$this->Aircraft_ID){
        if (count ($AircraftImage) > 0){
        //set_time_limit (240);
        $q = mysql_query ("SELECT MAX(Aircraft_ID) as id FROM `aircraft` WHERE `IsDeleted` = 0 AND
         `User_ID` = '".$this->User_ID."'");
        $res = mysql_fetch_object ($q);
        $maxid = $res->id;
        foreach ($AircraftImage as $k=>$v){ 
            $split    = explode('::', $v);  
            $fullpath = "http://localhost/pz_serveroriginal/uploads/".$split[0];            
            //$fullpath = "http://pilotzeus.com/pz/uploads/".$split[0]; 
            $filesize = $split[1];  
        // Insert into aircraft image with full path...
        // select maxid from aircraft

        $insert_query = mysql_query ("INSERT INTO `aircraft_image` (`Aircraft_ID`,`uploadpath`,`filesize`)
        VALUES ('".$maxid."','".$fullpath."','".$filesize."')");
        if ($insert_query == FALSE){
            throw new Exception ("Some Error Occurred while Saving Images");
        }           

    }
    }

    }

    else{   
        if (count ($AircraftImage) > 0){
            //DELETE ALL
            $deletequery = mysql_query ("DELETE FROM `aircraft_image` WHERE `Aircraft_ID` = '".$this->Aircraft_ID."' ");
            //set_time_limit (240);
            foreach ($AircraftImage as $k=>$v){
            $split    = explode('::', $v);                          
            $fullpath = "http://localhost/pz_serveroriginal/uploads/".$split[0];            
            //$fullpath = "http://pilotzeus.com/pz/uploads/".$split[0];     
            $filesize = $split[1];  

            //check if current image is not equal to database image then insert into database
            // if image already exists or not

            $insert_query = mysql_query ("INSERT INTO `aircraft_image` (`Aircraft_ID`,`uploadpath`,`filesize`)
            VALUES('".$this->Aircraft_ID."','".$fullpath."','".$filesize."')");
            if ($insert_query == FALSE){
                throw new Exception ("Some Error Occurred while updating images");
            }                               
    }
}
    }

}

1 个答案:

答案 0 :(得分:0)

更新数据时,有两种方法可以将图像更新为aircraft_image表,

1) You update all previous images by ID (Primary key)
           OR
2) You deleted all previous images for that particular Aircraft_ID and insert all new images for that.

更新:

  

为此,您需要使用jquery删除该特定行   ($('rowid')。remove()),所以当你从表中删除任何记录时   它也从HTML中删除该行。现在当你刷新那个页面时   它显然不会显示那一行。

如果您有任何查询/问题,请告诉我

谢谢!