除了$ sqlImg(应该将上传的图像插入表格)之外的所有内容都正常工作。有人可以向我解释为什么它不起作用?或者,如果有另一种方式可行,我很乐意听到它。下面的文件是我的upload.inc.php。我打算在将一个名为home.php的文件插入我的桌面后回显该图像。
<?php
if (!isset($_SESSION)) {
session_start();}
include_once 'includes/dbh.inc.php';
$id = $_SESSION['key'];
if (isset($_POST['submitFile'])) {
$file = $_FILES['file'];
$fileName = $file['name'];
$fileTmpName = $file['tmp_name'];
$fileSize = $file['size'];
$fileError = $file['error'];
$fileType = $file['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png');
if (in_array($fileActualExt, $allowed)) {
if ($fileError == 0) {
if ($fileSize < 1000000) {
$fileNameNew = "profile".$id.".".$fileActualExt;
$fileDestination = 'uploads/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
$sql = "UPDATE profileimg SET status=0 WHERE userid='$id';";
mysqli_query($conn, $sql);
$sqlImg = "INSERT INTO profileimg (image) WHERE userid='$id' VALUES ($fileNameNew);";
mysqli_query($conn, $sqlImg);
header("Location: home.php?upload=success");
}
else { echo "Sorry, your file size is too big.";}
}
else { echo "Oops, an error occurred!";}
}
else { echo "Please upload png and jpg files only.";}
}
答案 0 :(得分:2)
您正在对新图片进行INSERT
。我认为你需要UPDATE
。
更改:强>
$sql = "UPDATE profileimg SET status=0 WHERE userid='$id';";
要强>
$sql = "UPDATE profileimg SET status=0, image = '$fileNameNew' WHERE userid='$id';";
并删除:
$sqlImg = "INSERT INTO profileimg (image) WHERE userid='$id' VALUES ($fileNameNew);";
mysqli_query($conn, $sqlImg);
你正在以错误的方式进行session_start()。它应该一直被调用。
更改:强>
if (!isset($_SESSION)) {
session_start();}
要强>
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
答案 1 :(得分:0)
$sqlImg = "INSERT INTO profileimg (image) WHERE userid='$id' VALUES ($fileNameNew);";
mysqli_query($conn, $sqlImg);
应该是
$sqlImg = "INSERT INTO profileimg (image) WHERE userid=".$id." VALUES (".$fileNameNew.")";
mysqli_query($conn, $sqlImg);