如何通过jquery

时间:2016-10-12 18:05:41

标签: php jquery mysqli

我正在尝试通过表单将图像上传到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

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

2 个答案:

答案 0 :(得分:1)

你必须使用&#34; enctype&#34;在形成标签。

这是一个例子。

<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>

答案 1 :(得分:0)

使用以下内容:

$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
}

然后执行!