【问题标题】:How do I upload images in mysqli through jquery如何通过jquery在mysqli中上传图片
【发布时间】:2016-10-12 18:05:41
【问题描述】:

我正在尝试通过表单将图像上传到 mysql 这是我的表单

<form action="" method="POST" id="formSettingStoreLogo">
    <div>
        <p>Put a logo </p><br>
        <input type="file" name="logo" />
        <input type="submit" name="submitLogo" id="submitLogo" value="Upload" />
    </div>
</form>

这是我写的jQuery

$(document).on('submit', '#formSettingStoreLogo', function(e) 
{
    e.preventDefault();
    $.ajax({
        type: 'POST',
        url: logoupload.php,
        data: $("#formSettingStoreLogo").serialize(),
        success: function(data) 
        {
        }
    });
};

和 logoupload.php

<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';

sec_session_start();
$storeid = $_SESSION['storeid'];
$stmt = $mysqli->prepare("UPDATE store_details SET store_logo=? WHERE store_id =?");
$stmt->bind_param('bi',$_POST['logo'],$storeid);
$stmt->execute();
$stmt->store_result();
?>

<p>Uploading logo of store</p>

还有

blob is the data type of store_logo column

表单发布数据但不插入数据库。

【问题讨论】:

标签: php jquery mysqli


【解决方案1】:

你必须使用“enctype”来形成标签。

这里是例子。

<form action="demo_post_enctype.asp" method="post" enctype="multipart/form-data">
  <div>
        <p>Put a logo </p><br>
        <input type="file" name="logo" />
        <input type="submit" name="submitLogo" id="submitLogo" value="Upload" />
    </div>
</form>

【讨论】:

    【解决方案2】:

    使用以下内容:

    $fp = fopen($_FILES['logo']['tmp_name'], "r");
    
    // If successful, read from the file pointer using the size of the file (in bytes) as the length.
    if ($fp) {
        $content = fread($fp, $_FILES['file']['size']);
        fclose($fp);
        $stmt->bind_param('bi',$content,$storeid);
    }
    else {
        // Some error occured
    }
    

    然后执行!

    【讨论】:

    • 在 logoupload.php 文件中?
    • 也添加 encType (如另一个答案)
    猜你喜欢
    • 2017-11-01
    • 2017-03-13
    • 2017-11-17
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    相关资源
    最近更新 更多