Title1

Title2

Title3

12-1 商品更新

  1. 流程:
    
      #商品更新
      case "opUpdate" :
        $msg = opUpdate($sn);
        redirect_header($_SESSION['returnUrl'], 3000, $msg);
        exit;
    

     

  2. 程式:
    
    #################################################
    #  商品更新
    #################################################
    function opUpdate($sn){
      global $db;
      if(!$sn) redirect_header($_SESSION['returnUrl'], 3000, "商品編號錯誤!");
      
      #驗證token
      verifyToken($_POST['token']);
    
      #過濾
      $_POST['title'] = db_CleanVars($_POST['title'], "商品名稱");
      $_POST['kind'] = db_CleanVars($_POST['kind'], "類別");
      $_POST['price'] = db_CleanVars($_POST['price'], "");//商品價格
      $_POST['amount'] = db_CleanVars($_POST['amount'], "");//商品數量
      $_POST['enable'] = db_CleanVars($_POST['enable'], "啟用");
      $_POST['choice'] = db_CleanVars($_POST['choice'], "精選");
      $_POST['date'] = db_CleanVars($_POST['date'], "建立日期");
      $_POST['date'] = strtotime($_POST['date']);
    
      $_POST['sort'] = db_CleanVars($_POST['sort'], "排序");
      $_POST['icon'] = db_CleanVars($_POST['icon'], "");//圖示
      $_POST['summary'] = db_CleanVars($_POST['summary'], "");//商品摘要
      $_POST['content'] = db_CleanVars($_POST['content'], "");//商品內容
    
      #更新資料庫
      $sql = "update `ugm_p_prod` set 
              `title` = '{$_POST['title']}',
              `kind` = '{$_POST['kind']}',
              `price` = '{$_POST['price']}',
              `amount` = '{$_POST['amount']}',
              `enable` = '{$_POST['enable']}',
              `choice` = '{$_POST['choice']}',
              `date` = '{$_POST['date']}',
              `sort` = '{$_POST['sort']}',
              `icon` = '{$_POST['icon']}',
              `summary` = '{$_POST['summary']}',
              `content` = '{$_POST['content']}'
              where `sn` = '{$sn}'"; //die($sql);
      
      $db->query($sql) or redirect_header("", 3000,  $db->error."\n".$sql,true);
    
      #上傳圖片
      if($_FILES['pic']['tmp_name'] and !$_FILES['pic']['error']){
        #先刪除圖片
        deleteFiles_center("prod",$sn);
       
        #處理新檔名
        $ext_name = strrchr($_FILES['pic']['name'], "."); //取得副檔名 .jpg,請注意有包含點
        $rand = substr(md5(uniqid(mt_rand(), 1)), 0, 5);//取得一個5碼亂數
        $new_name = $rand ."_".$sn.$ext_name;
    
        #檢查資料夾
        $DirName = "prod";
        mk_dir(WEB_PATH . "/uploads/{$DirName}");
        mk_dir(WEB_PATH . "/uploads/{$DirName}/pic");
    
        #上傳
        if(move_uploaded_file($_FILES['pic']['tmp_name'] , WEB_PATH . "/uploads/{$DirName}/pic/{$new_name}")){
          #上傳成功,把圖片資訊寫入圖片檔 
          $sql = "insert into `ugm_p_files_center`
                (`col_name`,`col_sn`,`sort`,`kind`,`file_name`,`file_type`,`file_size`,`description`,`original_filename`,`sub_dir`) values
                ('{$DirName}','{$sn}','1','img','{$new_name}','{$_FILES['pic']['type']}','{$_FILES['pic']['size']}','{$_POST['title']}','{$_FILES['pic']['name']}','{$DirName}/pic')"; //die($sql);
    
          $db->query($sql) or redirect_header("", 3000,  $db->error."\n".$sql,true);
          
        }else{
          return "商品圖片上傳失敗!!";
        }
    
      }
      return "編輯商品成功!!";
      
    }

     

  3. 刪除圖片
    
    #################################################
    #  刪除圖片
    #################################################
    function deleteFiles_center($col_name,$col_sn){
      global $db;
      #取得記錄
      $Files=getFilesBYcol_nameCol_sn($col_name,$col_sn); 
      #將檔案刪除
      if(file_exists(WEB_PATH."/uploads/".$Files['sub_dir']."/".$Files['file_name'])){
        unlink(WEB_PATH."/uploads/".$Files['sub_dir']."/".$Files['file_name']);
      }  
      #刪除圖片檔資料
      $sql = "delete 
              from `ugm_p_files_center`
              where `files_sn`='{$Files['files_sn']}'
      ";
      $db->query($sql) or redirect_header("", 3000,  $db->error."\n".$sql,true);
    }

     

  4. 用「col_name」、「col_sn」得到圖片記錄
    
    function getFilesBYcol_nameCol_sn($col_name,$col_sn){
      global $db;
      #撈資料
      $sql = "select *
              from `ugm_p_files_center`
              where `col_name`='{$col_name}' and `col_sn`='{$col_sn}'
      ";
      $result = $db->query($sql) or redirect_header("", 3000,  $db->error."\n".$sql,true);
      $row = $result->fetch_assoc();
      return $row;
    }