简体   繁体   中英

How to store image link in database php/mysql

I'm trying to allow an admin upload pictures of products in to the database, but I only want to store the link/url of the picture in the database and then store the uploaded file in a folder. This is what I've got so far, and I keep getting " Sorry there was a problem uploading your file ".

Here is the PHP code:

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

and the HTML form:

<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

Use Sql Query Below.

$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")."')";

Also Change below line for upload image

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

First of all in HTML form action="post" is incorrect, the action attribute should contain a path. The method attribute should contain post or get like this: method="get" or method="post" .

Try this Hope this helps.Not tested

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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