Title1

Title2

Title3

18-5 op_insert()函數

  1. op_insert()
    ########################################
    # 新增記錄
    ########################################
    function op_insert()
    {
      global $mysqli,$TBL;
      #資料過濾
      #http://php.net/manual/en/mysqli.real-escape-string.php
      $_POST['title']  = $mysqli->real_escape_string($_POST['title']);
      $_POST['enable'] = intval($_POST['enable']);
      //$_POST['sort']   = intval($_POST['sort']);
      $_POST['sort']   = get_max_sort_show_prod("sort",$TBL);//直接寫入最大值
    
    
      $_POST['kind'] = intval($_POST['kind']);
      $_POST['date']  = $mysqli->real_escape_string($_POST['date']);
      $_POST['date']  = strtotime($_POST['date']);//轉成時間戳記
      $_POST['price'] = intval($_POST['price']);
      $_POST['summary']  = $mysqli->real_escape_string($_POST['summary']);
      $_POST['content']  = $mysqli->real_escape_string($_POST['content']);
    
      $sql = "insert into `{$TBL['name']}`
              (`title`, `enable`, `sort`,`kind`,`date`,`price`,`summary`,`content`)
              VALUES
              ('{$_POST['title']}', '{$_POST['enable']}', '{$_POST['sort']}', '{$_POST['kind']}', '{$_POST['date']}', '{$_POST['price']}', '{$_POST['summary']}', '{$_POST['content']}')";
      $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
    
      $sn=$mysqli->insert_id;//傳回insert 指令所產生之流水號
    
      /*
      $_FILES['file']['name']:上傳檔案原始名稱。
      $_FILES['file']['type']:檔案的 MIME 類型,例如“image/gif”。
      $_FILES['file']['size']:已上傳檔案的大小,單位為bytes。
      $_FILES['file']['tmp_name']:檔案被上傳後的臨時檔案名。
      $_FILES['file']['error']:和該檔案上傳相關的錯誤代碼。
       */
      if($_FILES['file'] and !$_FILES['file']['error'])
      {
        $col_name="prod";//商品圖的關鍵字 prod
        $col_sn=$sn;//相關流水號
        $sort=1;//排序
        $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);//取得副檔名
        $ext = strtolower($ext); //轉小寫
        $sub_dir="/prod";//儲存位置
        $file_name="prod_".$col_sn."_".$sort.".".$ext;//檔名
    
        if($ext == "png" or $ext == "gif" or $ext == "jpg" or $ext == "jpeg" or $ext == "jpe")
        {
          $kind="img";
        }else
        {
          $kind="file";
        }
        #寫入資料表 show_files
        $sql = "insert into `show_files`
                (`col_name`, `col_sn`, `sort`, `kind`,`file_name`,`file_type`,`file_size`,`description`,`sub_dir`)
                VALUES
                ('{$col_name}', '{$col_sn}', '{$sort}', '{$kind}', '{$file_name}', '{$_FILES['file']['type']}','{$_FILES['file']['size']}', '{$_POST['title']}', '{$sub_dir}')";
        $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
        $new_file= WEB_PATH."/uploads".$sub_dir."/".$file_name;
        #移動檔案
        move_uploaded_file($_FILES['file']['tmp_name'] , $new_file);
      }
    
      return $sn;
    }

     

  2. get_max_sort_show_prod() => function.php
    #####################################################################################
    #  自動取得商品檔(排序欄位,資料表)的最新排序
    #  get_max_sort_show_prod($col,$TBL)
    #  (排序欄位,資料表)#
    #####################################################################################
    if(!function_exists("get_max_sort_show_prod")){
    function get_max_sort_show_prod($col="sort",$TBL=""){
      global $mysqli;
      if(empty($TBL))return;
      $sql = "select max({$col})
              from `{$TBL['name']}`";
      $result = $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
    
      list($sort)=$result->fetch_row();
      return ++$sort;
    }
    }

     

  3.