query 文件提交 +php后台入库

php代码

<?php 
if($_POST){
          $post['name'] = $_POST['name']; //获取索引
          $post['age'] = $_POST['age'];
          $post['sex'] = $_POST['sex'];
          $filesName = $_FILES['file']['name'];  //文件名数组
          $filesTmpName = $_FILES['file']['tmp_name'];  //临时文件名数组
          $filePath = DT_ROOT.'/file/'.$filesName; //文件路径
          if(move_uploaded_file($filesTmpName, $filePath)){
            $post['thumb']= DT_PATH.'file/'.$filesName; 
         }
          foreach ($post as $key => $value){
              $k[]=$key;
              $v[]=$value;
          }
          $vs ="'" . implode("','", $v). "'";
          $ks=implode(',', $k);
          $res=$db->query("INSERT INTO `{$DT_PRE}t_types` ($ks) VALUES ($vs)");
          if($res){
              echo 1;
          }else{
              echo 2;
          }                
}
html 代码

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8" />
    <title>文件上传</title>
  </head>

  <body>
    
    <form id="test">
      <input type="text" name="name">
      <input type="text" name="age">
      <input type="text" name="phone">
      <input type="file" name="file">
      <input type="button" onclick="add()" value="新增">
    </form>
    
  </body>
  
  <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

  <script type="text/javascript">
    function add(){
      var formData = new FormData(document.querySelector("#test"));//获取form值
      $.ajax({
        url: "http://www.she.com/admin.php?moduleid=31&file=app&action=type_add",
        type: "POST",
        data: formData,
        processData: false,  // 不处理数据
        contentType: false,   // 不设置内容类型
        success:function(data){
          alert(data);
        }
      });
    }
  </script>
</html>  

 

posted @ 2020-06-30 16:12  穷帅哥依然纵横一方  阅读(183)  评论(0编辑  收藏  举报