小程序表单 和多张图片上传 ,php接收表单信息并且把图片存入服务器文件夹,图片路径存入数据库

var adds = {};
Page({
  data: {
    img_arr: [],
    formdata: '',
  },
  formSubmit: function (e) {
    var id = e.target.id
    adds = e.detail.value;
    adds.nowTime =new Date().getTime();
    this.upload()
  },

  upload: function () {
    var that = this
    for (var i = 0; i < this.data.img_arr.length; i++) {
      adds.index = i;
      console.log(adds)
      wx.uploadFile({
        url: 'http://localhost/upImg/up_img.php',
        filePath: that.data.img_arr[i],
        name: 'file',
        formData: adds,
        success: function (res) {
          console.log('1111111111',res)
          if (res) {
            wx.showToast({
              title: '已提交发布!',
              duration: 3000
            });
          }
        }
      })
    }
    this.setData({
      formdata: ''
    })
  },
  upimg: function () {
    var that = this;
    if (this.data.img_arr.length < 3) {
      wx.chooseImage({
        sizeType: ['original', 'compressed'],
        success: function (res) {
          that.setData({
            img_arr: that.data.img_arr.concat(res.tempFilePaths)
          })
        }
      })
    } else {
      wx.showToast({
        title: '最多上传三张图片',
        icon: 'loading',
        duration: 3000
      });
    }
  },  
})

2

姓名: 电话:

3

 0){  
	  echo "错误: " . $_FILES["file"]["error"] . "
"; } $name = $_POST['name']; $phone = $_POST['phone']; $index = $_POST['index']; $nowTime = $_POST['nowTime']; $img = $_FILES['file']['name']; if($index==0){ //插入数据到数据库 $sql = "insert into test (name,phone,img,nowTime) values('$name','$phone','$img','$nowTime')"; $retval = mysqli_query( $conn, $sql ); if(! $retval ) { die('无法插入数据: ' . mysqli_error($conn)); } echo "数据插入成功\n"; mysqli_close($conn); move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); }else{ //提交图片 $img = $_FILES['file']['name']; $result = mysqli_query($conn,"SELECT * FROM test WHERE nowTime='$nowTime'"); $ab; while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { if($row["img"]!=""){ $ab =$row["img"].','.$img; }else{ $ab =$img; } //修改数据库字段 mysqli_query($conn,"UPDATE test SET img = '$ab' WHERE nowTime = '$nowTime'"); echo $ab; } move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); } ?>

 

你可能感兴趣的:(PHP,微信小程序,Web前端)