我正在尝试使用BLOB将图像保存到我的数据库 - 我知道通过filepath这样做更好,但我打算使用BLOB进行测试。
我编写了以下代码 - 除了图像之外,所有内容都存储在数据库中,我没有错误。
如何将图像存储在数据库中?
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);