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

如何在php/mysql数据库中存储图像链接,php,html,mysql,Php,Html,Mysql,我试图允许管理员将产品图片上传到数据库中,但我只想将图片的链接/url存储在数据库中,然后将上传的文件存储在文件夹中。 这就是我到目前为止所得到的,我一直得到“很抱歉上传你的文件时出现问题” 以下是PHP代码: if ($_FILES['product_image']['error'] == 0) { // checking the file for any errors $imgName = mysql_real_escape_string($_FILES['product_image

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

产品图片*:
添加项

首先,在HTML
formaction=“post”
中,如果不正确,则
action
属性应包含路径。
method
属性应该包含
post
get
这样的内容:
method=“get”
method=“post”
首先在HTML
form action=“post”
中不正确,
action
属性应该包含路径。
method
属性应该包含
post
get
这样的内容:
method=“get”
method=“post”
首先在HTML
form action=“post”
中不正确,
action
属性应该包含路径。
method
属性应该包含
post
get
这样的内容:
method=“get”
method=“post”
首先在HTML
form action=“post”
中不正确,
action
属性应该包含路径。
method
属性应该包含如下内容的
post
get
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!";
        }
?>

试试这个,希望有帮助。未经测试

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

试试这个,希望有帮助。未经测试

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

试试这个,希望有帮助。未经测试

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

使用下面的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=$_文件[“产品_图像”][“tmp_名称”]

使用下面的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=$_文件[“产品_图像”][“tmp_名称”]

使用下面的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=$_文件[“产品_图像”][“tmp_名称”]

使用下面的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=$_文件[“产品_图像”][“tmp_名称”]

if(移动上传的文件($imgData,$targetFolder.$imgName)){
$targetFolder
中删除引号,然后重试。看起来您将
$imgName
添加到
$targetFolder
中两次–首先在顶部的if语句中,然后在
移动上传的文件中再次添加。当然,您处理转义错误(因为您正在将
mysql\u real\u escape\u string
应用于一个值,该值随后用作文件名)。您正在读取上传文件的内容,并尝试稍后将此数据作为第一个参数传递给
移动上传文件
,这也是胡说八道……您应该开始读取手册中实际需要的参数,而不是通过试错(至少现在看起来您正在执行后者)@ICanHasCheezburger:我认为这不会引起问题,因为变量仍然会作为字符串的一部分进行解析,但是为了保持清楚,我想最好删除引号
if(move_uploaded_file($imgData,$targetFolder.$imgName)){
$targetFolder
中删除引号,然后重试。看起来您将
$imgName
添加到
$targetFolder
中两次–首先在顶部的if语句中,然后在
移动上传的文件中再次添加。当然,您处理转义错误(因为您正在将
mysql\u real\u escape\u string
应用于一个值,该值随后用作文件名)。您正在读取上传文件的内容,并尝试稍后将此数据作为第一个参数传递给
移动上传文件
,这也是胡说八道……您应该开始读取手册中实际需要的参数,而不是通过试错(至少现在看起来您正在执行后者)@ICanHasCheezburger:我认为这不会引起问题,因为变量仍然会作为字符串的一部分进行解析,但是为了保持清楚,我想最好删除引号
if(move_uploaded_file($imgData,$targetFolder.$imgName)){
$targetFolder
中删除引号,然后重试。看起来您将
$imgName
添加到
$targetFolder
中两次–首先在顶部的if语句中,然后在
移动上传的文件中再次添加。当然,您处理转义错误(因为您正在将
mysql\u real\u escape\u string
应用于一个值,该值随后用作文件名)。您正在读取上传文件的内容,并尝试稍后将此数据作为第一个参数传递给
移动上传文件
,这也是胡说八道……您应该开始读取手册中实际需要的参数,而不是通过试错(至少现在看起来您正在执行后者)@ICanHasCheezburger:我认为这不会引起问题,因为变量仍然会作为字符串的一部分进行解析,但是为了保持清楚,我想最好删除引号
if(move_uploaded_file($imgData,$targetFolder.$imgName)){
$targetFolder
中删除引号,然后重试。看起来您要将
$imgName
附加到
$targetFolder
中两次–第一次在if sta中
$imgData = mysql_real_escape_string(file_get_contents($_FILES["product_image"]["tmp_name"]));