一个值存储在数据库中,但它不是我期望的那个。我之前尝试了很多方法,但这种方法似乎有效,但文件名没有存储,当我尝试直接从数据库下载文件时,它下载的.bin文件格式类似于table_Name-column_Name.bin 。存储的文件名是BLOB - ## B。
我的表格
<form class="form-horizontal" method="post" action="productsValidate.php" name="myForm" enctype="multipart/form-data">
<fieldset>
<legend>Add Product</legend>
<div class="form-group">
<label for="Product_Name" class="col-lg-2 control-label">Product Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="Product_Name" placeholder="Name" required="required" name="Product_Name">
</div>
</div>
<div class="form-group">
<label for="Size" class="col-lg-2 control-label">Size</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="Size" placeholder="Size" required="required" name="Size">
</div>
</div>
<div class="form-group">
<label for="Color" class="col-lg-2 control-label">Color</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="Color" placeholder="Size" required="required" name="Color">
</div>
</div>
<div class="form-group">
<label for="price" class="col-lg-2 control-label">Price</label>
<div class="col-lg-10">
<input type="number" class="form-control" id="price" placeholder="price" required="required" name="price">
</div>
</div>
<div class="form-group">
<label for="image" class="col-lg-2 control-label">Select Image</label>
<div class="col-lg-10">
<input type="file" name="image" id="image">
</div>
</div>
<div class="form-group">
<label for="categoryId" class="col-lg-2 control-label">Category Id</label>
<div class="col-lg-10">
<?php
//your connection to the db and query would go here
include "../include/settings.php";
$conn = new mysqli($host, $user, $pwd, $sql_db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT distinct Category_Id FROM products";
$result = mysqli_query($conn, $sql);
?>
<select id="categoryId" name="categoryId">
<option value = ""></option>
<?php
while($row = mysqli_fetch_array($result)) {
echo '<option value='.$row['Category_Id'].'>'.$row['Category_Id'].'</option>';
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="description" class="col-lg-2 control-label">Description</label>
<div class="col-lg-10">
<textarea type="text" class="form-control" id="description" placeholder="Description" required="required" name="description" pattern="[\sA-Za-z]+"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-lg-6 col-lg-offset-2">
<button type="submit" class="btn btn-primary">Add Product</button>
</div>
</div>
</fieldset>
</form>
我的表单验证
<?php
$name = $_POST["Product_Name"];
$size = $_POST["Size"];
$color = $_POST["Color"];
$price = $_POST["price"];
$image = addslashes($_FILES['image']['tmp_name']);
$image = file_get_contents($image);
$image = base64_encode($image);
$image=basename( $_FILES["image"]["tmp_name"],".jpg");
$category = $_POST['categoryId'];
$description = $_POST['description'];
insertProduct($name, $size, $color, $price, $image, $category, $description);
function insertProduct($name, $size, $color, $price, $image, $category, $description){
require_once ("../include/settings.php"); // Load MySQL log in credentials
$conn = @mysqli_connect ($host,$user,$pwd,$sql_db); // Log in and use database
if ($conn) { // check is database is avialable for use
$query = "INSERT INTO products
(Product_Id, Product_Name, Size, Color, Price, Picture, Category_Id, Description)
VALUES ('', '$name', '$size', '$color', '$price', '$image', '$category', '$description')";
$result = mysqli_query ($conn, $query);
if ($result) { // check if query was successfully executed
echo 'Successfully Added';
} else {
echo 'Product could not be added';
}
mysqli_close ($conn); // Close the database connect
} else {
echo "<p>Unable to connect to our database for adding the product.</p>";
}
}
?>
答案 0 :(得分:2)
我猜你试图将实际的编码图像存储在数据库中,而不是指向它的指针。在我看来,你的十一字节BLOB中有指针。
您的代码包含此序列。
$image = addslashes($_FILES['image']['tmp_name']);
$image = file_get_contents($image);
$image = base64_encode($image);
$image=basename( $_FILES["image"]["tmp_name"],".jpg");
第三行将图像的编码而非二进制版本放入文本字符串中。这与你想要的很接近,但如果你输入BLOB,你可能不应该对其进行64位编码。
第四行丢弃图像本身并用图像名称覆盖它。我认为那是错的。
如果你打算以这种方式使用BLOB数据,你还需要使用mysqli的工具来prepare你的SQL语句,然后bind你的参数。 bind_param()
函数为您提供了一种将参数声明为blob的方法。这比尝试欺骗php的字符串处理接受它更好。
尽管如此,大多数人使用文件系统或内容服务器而不是BLOB来存储和向Web客户端提供图像。 BLOB编程是一个痛苦的问题。此外,使用DBMS快速存储和检索图像成为扩展应用程序的性能瓶颈。