繁体   English   中英

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

[英]How to store image link in database php/mysql

我试图允许管理员将产品图片上传到数据库中,但是我只想将图片的链接/ 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”]; 

首先,HTML form action="post"不正确, action属性应包含路径。 method属性应包含postget例如: method="get"method="post"

试试这个希望对您有所帮助。未经测试

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

<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>

PHP代码:

<?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)) { // 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!";
        }
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM