-2

I am trying to upload an image through form into mysql this is my form

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

This is the jQuery I have written

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

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

And

blob is the data type of store_logo column

Form posts data but doesn't insert into database.

RamenChef
  • 5,557
  • 11
  • 31
  • 43
  • You can't upload image using this ajax call to send files you need to use [FileAPI](https://developer.mozilla.org/en/docs/Using_files_from_web_applications) – itzmukeshy7 Oct 13 '16 at 05:43
  • Possible duplicate of [jQuery Ajax File Upload](http://stackoverflow.com/questions/2320069/jquery-ajax-file-upload) – itzmukeshy7 Oct 13 '16 at 05:45

2 Answers2

1

you have to use "enctype" in to form tag.

here is example.

<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>
Milan Parekh
  • 163
  • 1
  • 11
0

Use the following:

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

Then Execute!

Praveen
  • 419
  • 3
  • 14