/    Sign up×
Community /Pin to ProfileBookmark

Issue with the function "move_uploaded_file"

Hey there.

Basically I want to upload images that I insert into my database via php.

This is the code relevant to the php part.

[QUOTE]

<?php

if(isset($_POST[‘insert_post’])){

//getting the text data from the fields
$product_title = $_POST[‘product_title’];
$product_cat = $_POST[‘product_cat’];
$product_manufacturer = $_POST[‘product_manufacturer’];
$product_price= $_POST[‘product_price’];
$product_desc = $_POST[‘product_desc’];
$product_keywords = $_POST[‘product_keywords’];

//getting the image from the field product_image
$product_image = $_FILES[‘product_image’][‘name’];
$product_image_tmp = $_FILES[‘product_image’][‘tmp_name’];

move_uploaded_file($product_image_tmp, ‘product_images/$product_image’);

$insert_product = “INSERT INTO products VALUES(”,’$product_title’,’$product_cat’,’$product_manufacturer’,’$product_price’,’$product_desc’,’$product_image’,’$product_keywords’)”;

$insert_stuff = mysqli_query($con, $insert_product);

if($insert_stuff){

echo”<script>alert(‘Product has been inserted’)</script>”;
echo”<script>window.open(‘insert_product.php’,’_self’)</script>”;
}

}

[/QUOTE]

The thing is, whenever I insert a new product, that exact same image replaces the former one, and so on. What I wanted is to have all the pictures of all the products I insert into the database being uploaded in that folder (product_images) and not replacing themselves 1 by 1.

Thanks in advance.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@KrauserauthorJul 03.2015 — Hey again, I've fixed the issue in the meantime. It was just the name of a variable not being the same as in the database.
Copy linkTweet thisAlerts:
@KrauserauthorJul 03.2015 — Actually, sorry about that, I still can't upload 2 or more images without replacing the image I have for other.

Need help, thanks in advance.
Copy linkTweet thisAlerts:
@NogDogJul 03.2015 — You won't interpolate $product_image in this code:
[code=php]
move_uploaded_file($product_image_tmp, 'product_images/$product_image');
[/code]

You need to either use double quotes (since variable interpolation does not occur within a single-quoted string), or else concatenation.
Copy linkTweet thisAlerts:
@KrauserauthorJul 03.2015 — Thank you, that has resolved the issue.
×

Success!

Help @Krauser spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...