我一直在研究图像上传器将图像上传到保存数据库中的详细信息的文件,问题是在我上传图像后上传一张图像时它会覆盖当前上传的图像。此外,我试图使用新的图像名称存储新图像,但它似乎只是将其存储为“.jpg”。这是我的流程代码;
<?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();
}
?>
答案 0 :(得分:0)
使用它。它现在工作正常。(我假设你的表有一个自动生成的id字段。)
$pid = mysqli_insert_id($con);
答案 1 :(得分:0)
尝试更改
$newName = "$pid.jpg";
要
$newName = $pid . ".jpg";
答案 2 :(得分:0)
必须将mysqli_insert_id()更改为mysqli_insert_ID($ con),初始代码为mysql_insert_id(),您无需添加连接。我也忘了将id更改为ID以匹配我的数据库名称。
$pid = mysqli_insert_ID($con);
$newName = "$pid.jpg";
move_uploaded_file($_FILES['fileField']['tmp_name'],"blogPhotos/$newName");
header("location: datauploadprocess.php");
exit();
}
感谢您的帮助。