简体   繁体   English

将图像上传到存储详细信息在数据库中的文件,但它将覆盖已经上传的图像

[英]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'. 我也试图用新的图像名称存储新图像,但它似乎只是将其存储为“ .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.) 它现在可以正常工作。(我假设您的表有一个自动生成的id字段。)

$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. 不得不将mysqli_insert_id()更改为mysqli_insert_ID($ con),初始代码为mysql_insert_id(),您无需添加连接。 I also forgot to change id to ID to match my database name. 我也忘记将ID更改为ID以匹配我的数据库名称。

$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. 感谢帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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