0

I am trying to save an image to my db using a BLOB - I know it's better to do so by filepath but it is my intention to do it using a BLOB for testing purposes.

I have written the following code - everything store in the db except the image and I get no errors.

How can I get the image to store in the DB?

INDEX.HTML:

<form action="saveToDatabase.php" method="post" enctype="multipart/form-data" >

    <p>What group does the event belong to? </p>

    <select name="group" class="form-control">
        <option>Red</option>
        <option>Yellow</option>
        <option>Green</option>
        <option>Red</option>
        <option>Purple</option>
    </select>
    <br>

    <!--Here is the image upload-->

    <h3>Upload a Picture</h3>

    <p>Select image to upload: </p>
    <br>

    <input type="file" class="filestyle" name="image">

    <h3>Date of Achievement</h3> 
    <p>Please enter the date in which this achievement was made(it does not have to be exact).

    <div class="input-append date" id="dp3" data-date="12-02-2012" data-date-format="dd-mm-yyyy">
        <input class="span2" size="16" type="text" value="12-02-2012">
        <span class="add-on">
            <i class="icon-th"/>
        </span>
    </div>
    <br>

    <h3>Description</h3>

    <p>Please enter text to describe the achievement (300 Characters Max) </p>
    <div class="form-group">
        <label for="comment">Comment:</label>
        <textarea class="form-control" rows="5" id="comment" name="about" />
    </div>
    <input type="submit" class="btn btn-info" value="Submit Button" name="submit">
</form>

saveToDatabase.php:

$connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server
$db = mysql_select_db("timelineinfo", $connection); // Selecting Database from Server
if (isset($_POST['submit'])) {
    // Fetching variables of the form which travels in URL
    $group = $_POST['group'];
    $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $about = $_POST['about'];
    //$date = $_POST['date'];
    if ($group !=''||$img !='' ||$date !=''||$about !=''){
        //Insert Query of SQL
        $query = mysql_query("insert into basic_upload(assigned_group, img, about) values ('$group',  '$image', '$about')");
        echo "<br/><br/><span>Data Inserted successfully.
        <a href='index.html'>Click to view timeline</a></span>";
    }
    else {
        echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
    }
}
mysql_close($connection); // Closing Connection with Server
error_reporting(E_ALL);
David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
aiapps
  • 15
  • 1
  • 6
  • 2
    Use [mysqli_\*](http://php.net/manual/en/book.mysqli.php) tho instead of mysl_\* – Tanuel Mategi Nov 19 '15 at 10:50
  • the image you are trying to store might have longer data string than BLOB max limit. Use `longtext` or `longblob`. I would suggest using `longtext`. It supports nearly 4GB of Data strings – Ashish Choudhary Nov 19 '15 at 11:00
  • Are you surprised you're not getting any errors, when you're enabling error reporting **after** your code? It doesn't make a sense. What're you expecting? – David Ferenczy Rogožan Nov 19 '15 at 11:10

0 Answers0