php file插入,jquery-file-upload 的php mysql插入问题

最近用jquery-file-upload 来改善网站上传的体验

结果上传后,也能成功插入,json传回页面一切正常!

但问题来了,他的sql 架构…有个叫url

但作者好像在PHP的SQL中没有处理

那我就改改吧,….

先新增了一些基本配置

$dir = $_COOKIE["uid"].'/'.date("Y").'/'.date("m").'/'.date("d").'/';$dirUP = "../../../att/".$dir;$dirLink = $dir;$options=array( 'upload_dir' => $dirUP, 'upload_url' => $dirLink, 'delete_type' => 'POST', 'db_host' => 'localhost', 'db_user' => 'root', 'db_pass' => '*****', 'db_name' => '*****', 'db_table' => 'files');

应该就是这段了….

我尝试多次,加入url字段都不成功 [原本的文档代码]

protected function handle_file_upload($uploaded_file, $name, $size, $type, $error, $index = null, $content_range = null) { $file = parent::handle_file_upload( $uploaded_file, $name, $size, $type, $error, $index, $content_range ); if (empty($file->error)) { $sql = 'INSERT INTO `'.$this->options['db_table'] .'` (`name`, `size`, `type`, `title`, `description`)' .' VALUES (?, ?, ?, ? , ?)'; $query = $this->db->prepare($sql); $query->bind_param( 'sisss', $file->name, $file->size, $file->type, $file->title, $file->description ); $query->execute(); $file->id = $this->db->insert_id; } return $file; }

都给我显示:

Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn’t match number of bind variables in

这是什么意思,说我的数量有问题? 是指我加少了吗?

我已经改成…这样,5处的type字段也都加了url也说是数量问题?

protected function handle_file_upload($uploaded_file, $name, $size, $type,$url, $error, $index = null, $content_range = null) { $file = parent::handle_file_upload( $uploaded_file, $name, $size, $type,$url, $error, $index, $content_range ); if (empty($file->error)) { $sql = 'INSERT INTO `'.$this->options['db_table'] .'` (`name`, `size`, `type`, `url`, `title`, `description`)' .' VALUES (?, ?, ?, ?,? , ?)'; $query = $this->db->prepare($sql); $query->bind_param( 'sisss', $file->name, $file->size, $file->type, $file->url, $file->title, $file->description ); $query->execute(); $file->id = $this->db->insert_id; } return $file; }

我需要保存成

$url = $_COOKIE[“uid”].’/’.date(“Y”).’/’.date(“m”).’/’.date(“d”).’/’. filename

要怎么改? 直接用$file->url就可以吗?

另外…因为这玩意,还弄到一个append取值问题,熟jq的朋友也可以去这帮帮小弟吧

http://bbs.csdn.net/topics/390862894

回复讨论(解决方案)

警告:mysqli_stmt::bind_param():在类型定义字符串不匹配的绑定变量的元素个数

这还不清楚吗?

$query->bind_param(

‘ sisss’, //怎么只有 5 个类型声明?

$file->name, //1

$file->size, //2

$file->type, //3

$file->url, //4

$file->title,//5

$file->description //6 共6个

);

警告:mysqli_stmt::bind_param():在类型定义字符串不匹配的绑定变量的元素个数

这还不清楚吗?

$query->bind_param(

‘ sisss’, //怎么只有 5 个类型声明?

$file->name, //1

$file->size, //2

$file->type, //3

$file->url, //4

$file->title,//5

$file->description //6 共6个

);

喔…原来这个问题

已成功解决了!

以前用完mysql_query后就转用pdo了,没用过mysqli,PDO好像就没怎么提过这写法

谢谢,学习了

HTML: ```html <!-- 显示当前图片 --> <img src="current_image.jpg" id="current-image"> <!-- 上传新图片 --> <input type="file" id="new-image"> <!-- 保存按钮 --> <button id="save-button">保存</button> ``` JS: ```js $(document).ready(function() { // 当选择新图片时,将其显示在页面上 $('#new-image').change(function() { var file = this.files[0]; var reader = new FileReader(); reader.onload = function(e) { $('#current-image').attr('src', e.target.result); } reader.readAsDataURL(file); }); // 当点击保存按钮时,将新图片上传到服务器 $('#save-button').click(function() { var file = $('#new-image').get(0).files[0]; var formData = new FormData(); formData.append('image', file); $.ajax({ url: 'upload.php', type: 'POST', data: formData, contentType: false, processData: false, success: function(response) { if (response.success) { // 更新数据库中的图片路径 var imagePath = response.imagePath; $.ajax({ url: 'update.php', type: 'POST', data: { imagePath: imagePath }, success: function(response) { if (response.success) { alert('保存成功'); } else { alert('保存失败'); } } }); } else { alert('上传失败'); } } }); }); }); ``` PHP: ```php <?php if ($_FILES['image']['error'] === UPLOAD_ERR_OK) { $fileName = $_FILES['image']['name']; $fileSize = $_FILES['image']['size']; $fileType = $_FILES['image']['type']; $fileTempName = $_FILES['image']['tmp_name']; $fileExtension = pathinfo($fileName, PATHINFO_EXTENSION); $allowedExtensions = array('jpg', 'jpeg', 'png', 'gif'); if (in_array($fileExtension, $allowedExtensions)) { $filePath = 'images/' . uniqid() . '.' . $fileExtension; if (move_uploaded_file($fileTempName, $filePath)) { echo json_encode(array( 'success' => true, 'imagePath' => $filePath )); } else { echo json_encode(array( 'success' => false )); } } else { echo json_encode(array( 'success' => false )); } } else { echo json_encode(array( 'success' => false )); } ?> ``` MySQL: ```sql UPDATE users SET image_path = 'new_image.jpg' WHERE id = 1; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值