Mustafa Alsuhaibi Mustafa Alsuhaibi - 5 months ago 12
PHP Question

How can I insert (or copy) an image from one table to another table

I want to copy the user image from users table

$query = "SELECT * from users WHERE username ='$username'";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
$user_image = $row['image'];


and insert the user image to testimonials table as ($testimonial_image )

$insert_query = "INSERT INTO testimonials (testimonial_author, testimonial_content, testimonial_image) ";
$insert_query .="VALUES ('$testimonial_author','$testimonial_content','$testimonial_image')";


How can I do that please??

Answer

When you do $user_image = $row['image']; try echo $user_image to see if you have got the data you expected.

If you have got the data you expected, try adding $testimonial_image=$user_image to your php code before

$insert_query = "INSERT INTO testimonials ...

This will make the value of the variable $testimonial_image the same as the value of $user_image that you have already retrieved.

If this does not work, there may be sql errors in one or both of the queries.

EDIT: FOLLOWING COMMENTS BELOW ABOUT DUPLICATING A FILE TO A NEW FOLDER:

To copy a flie, this has nothing to do with the sql:

copy("/images/user_image/".$user_image , "/images/testimonial_image/".$user_image)

see http://php.net/manual/en/function.copy.php

Comments