您的当前位置:首页正文

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

2024-11-25 来源:个人技术集锦
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

<form bindsubmit="formSubmit" id='2' bindreset="formReset">

  <view>姓名:
    <input type="text" class="input-text" name="name" placeholder="请输入姓名" />
  </view>
  <view>电话:
    <input type="text" class="input-text" name="phone" placeholder="请输入电话" />
  </view>

  <view class="big-logos">
    <image bindtap="upimg" src='../../img/jia.jpg'></image>
    <block wx:for="{{img_arr}}">
      <view class='logoinfo'>
        <image src='{{item}}'></image>
      </view>
    </block>
  </view>
  <button class='btn' formType="submit">发布</button>
</form>

3

<?php

	header("Content-Type:text/html;charset=utf8"); 
	header("Access-Control-Allow-Origin: *"); //解决跨域
	header('Access-Control-Allow-Methods:POST');// 响应类型  
	$dbhost = 'localhost';  // mysql服务器主机地址
	$dbuser = 'root';            // mysql用户名
	$dbpass = 'root';          // mysql用户名密码
	$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
	error_reporting(E_ALL & ~E_NOTICE);
	if(! $conn )
	{
	    die('Could not connect: ' . mysqli_error());
	}
	  
	mysqli_select_db( $conn, 'test' );
	
	if ($_FILES["file"]["error"] > 0){  
	  echo "错误: " . $_FILES["file"]["error"] . "<br />";  
	}
    $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"]); 
		
	}

		
?>   

 

显示全文