gpt4 book ai didi

php - 如何在数据库php/mysql中存储图片链接

转载 作者:太空宇宙 更新时间:2023-11-03 12:14:31 24 4
gpt4 key购买 nike

我试图让管理员将产品图片上传到数据库中,但我只想将图片的链接/url 存储在数据库中,然后将上传的文件存储在一个文件夹中。这是我目前所得到的,我不断收到“抱歉,上传您的文件时出现问题”。

这是 PHP 代码:

if ($_FILES['product_image']['error'] == 0) { // checking the file for any errors
$imgName = mysql_real_escape_string($_FILES['product_image']['name']); //returns the name of the image and stores it in variable $imgName
$imgData = mysql_real_escape_string(file_get_contents($_FILES["product_image"]["tmp_name"])); // returns the content of the file and stores it in $imgData
$imgType = mysql_real_escape_string($_FILES["product_image"]["type"]); //returns image/whatever the image type is

$targetFolder = "ProductImages/"; //directory where images will be stored...
$targetFolder = $targetFolder . basename($imgName); //adds the image name to the directory
}

$sql = "INSERT INTO products " . "(product_name,product_model,product_price,product_width,product_height,product_weight,product_quantity,product_category,product_subcategory, product_image, product_description,date_added) " . "VALUES('$product_name','$product_model','$product_price','$product_width','$product_height','$product_weight','$product_quantity', '$product_category', '$product_subcategory', '$imgName', '$product_description', NOW())";
//echo $sql;
mysql_select_db('online_store');
$result = mysql_query($sql, $conn);
$itemResult = "";
if (!$result) {
die('Could not enter data: ' . mysql_error());
}
$itemResult = "Product has been added";
if (move_uploaded_file($imgData, "$targetFolder" . $imgName)) { // writes/stores the image in the targetfolder->ProductImages
echo "The file " . basename($imgName) . "has been uploaded!";
} else {
echo "Sorry, there was a problem uploading your file!";
}

HTML 形式:

<form id="product_form" name="product_form" enctype="multipart/form-data" action="inventory_list.php" method="post">

<label for="product_image">Product Image*:</label> <input type="file" name="product_image"id="product_image"/>
</div>
<div>
<button name="add" id="add">Add Item</button>
</div>
</form

最佳答案

使用下面的 Sql 查询。

$sql = "INSERT INTO products(`product_name`,`product_model`,`product_price`,`product_width`,`product_height`,`product_weight`,`product_quantity`,`product_category`,`product_subcategory`,`product_image`,`product_description`,`date_added`) VALUES('".$product_name."','".$product_model."','".$product_price."','".$product_width."','".$product_height."','".$product_weight."','".$product_quantity."', '".$product_category."', '".$product_subcategory."', '".$imgName."', '".$product_description."','".date("Y-m-d H:i:s")."')";

同时更改下面的上传图片行

$imgData = mysql_real_escape_string(file_get_contents($_FILES["product_image"]["tmp_name"]));
$imgData = $_FILES["product_image"]["tmp_name"];

关于php - 如何在数据库php/mysql中存储图片链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22785713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com