[英]PHP error, cant insert image into database
我是 php 新手,不幸的是我嘗試將數據插入到我的數據庫中我成功插入了名稱和價格,但是我在插入圖片(LONGBLOB)時被卡住了,這是我的代碼
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "tbl_product";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(isset($_POST['update']))
{
$name = $_POST['name'];
$image = $_FILES['image'];
$price = $_POST['price'];
$statement = $conn->prepare('INSERT INTO tbl_product (name, image, price)
VALUES (:name, :image, :price)');
$statement->execute([
':name' => $name,
':image' => $image,
':price' => $price,
]);
}
?>
<div class="settings-row">
<h3>Name</h3>
<form action="insertscript.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<input type="text" class="form-control" name="name">
<h3>Image</h3>
Select image to upload:
<input type="file" name="image">
<h3>Price</h3>
<input type="number" class="form-control small-input" name="price" >
<input type="submit" value="Submit" name="update" id="update">
</div>
</form>
</div>
<? echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'" class="img-responsive"/>';?><br />
我認為你的數據庫和你的表不一樣..試試這個並替換'----datatable---'。 並且您必須使用 file_get_contents 將圖像轉換為 blob
<?php if (isset($_POST)) { $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "----datatable---"; $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); if(isset($_POST['update'])) { $name = $_POST['name']; $image = file_get_contents($_FILES['image']['tmp_name']); $price = $_POST['price']; $statement = $conn->prepare('INSERT INTO tbl_product (name, image, price) VALUES (:name, :image, :price)'); $statement->execute([ ':name' => $name, ':image' => $image, ':price' => $price ]); } } ?>
根據我發表的評論,您似乎希望存儲實際文件數據而不是文件路徑,因此在上傳的圖像上使用file_get_contents
- 也許像這樣......
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "tbl_product";
$conn = new PDO( "mysql:host=$servername;dbname=$dbname", $username, $password );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
/*
catch errors
*/
try{
/*
test that imprtant variables are set
*/
if( isset( $_POST['update'], $_POST['name'], $_POST['price'] ) && !empty( $_FILES['image'] ) ) {
$name = $_POST['name'];
$price = $_POST['price'];
/*
get reference to uploaded image
*/
$obj=(object)$_FILES['image'];
$tmp=$obj->tmp_name;
$error=$obj->error;
/*
if there were no errors with upload, proceed to insert into db
*/
if( $error == UPLOAD_ERR_OK && is_uploaded_file( $tmp ) ){
/*
as the column is a longblob it suggests that you wish to store the actual file rather than the path
- this will lead to a mahoosive database in quite a short time!!
*/
$image=base64_encode( file_get_contents( $tmp ) );
$statement = $conn->prepare('INSERT INTO tbl_product (`name`, `image`, `price`) VALUES (:name, :image, :price)');
$args=array(
':name' => $name,
':image' => $image,
':price' => $price
);
$result = $statement->execute( $args );
} else {
throw new Exception('Upload failed');
}
}
}catch( Exception $e ){
exit( $e->getMessage() );
}
?>
要顯示已保存為上述 base64 編碼數據的圖像,需要修改img
標簽的語法。 例如:
<img src='data:image/jpeg;base64, /9j/4AAQSkZJRgABAQEAYABgAAD//gA+Q1JFQVRPU...... etc etc
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.