简体   繁体   中英

Uploading an image to a file storing details in database but it overwrites the image already uploaded

I have been working on an image uploader to upload an image to a file saving the details in the database, the problem is after I upload one image the next time I upload an image it overwrites the current uploaded image. Also I was trying to store the new image with the new image name but it appears to just store it as '.jpg'. Here is my process code;

<?php
//Parser for Add Photo Form
if (isset($_POST["subject"], $_POST["content"], $_POST["imageName"])){
$subject = mysqli_real_escape_string ($con, ($_POST["subject"]));
$content = mysqli_real_escape_string ($con, ($_POST["content"]));
$imageName = mysqli_real_escape_string ($con, ($_POST["imageName"]));
$latitude = mysqli_real_escape_string ($con, ($_POST["latitude"]));
$longitude = mysqli_real_escape_string ($con, ($_POST["longitude"]));

//add photo to db
$sql = mysqli_query($con, "
           INSERT INTO
            Blog (
            subject,
            content,
            imageName,
            latitude,
            longitude,
            datetime
           )VALUES(
            '$subject',
            '$content',
            '$imageName',
            '$latitude',
            '$longitude',
            NOW())")
            or die(mysqli_error($con));
$pid = mysqli_insert_id();
$newName = "$pid.jpg";
move_uploaded_file($_FILES['fileField']['tmp_name'],"blogPhotos/$newName");
header("location: datauploadprocess.php");
exit();
}
?>

use this . it works fine now.(I assume that your table has an auto-generated id field.)

$pid = mysqli_insert_id($con);

Try changing

$newName = "$pid.jpg";

to

$newName = $pid . ".jpg";

Had to change mysqli_insert_id() to mysqli_insert_ID($con), the initial code was mysql_insert_id() where you do not need to add the connection. I also forgot to change id to ID to match my database name.

$pid = mysqli_insert_ID($con);
$newName = "$pid.jpg";
move_uploaded_file($_FILES['fileField']['tmp_name'],"blogPhotos/$newName");
header("location: datauploadprocess.php");
exit();
}

Thanks for help.

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