用户上传的图片未插入 table
Image uploaded by users is not being inserted to the table
除了 $sqlImg(应该将上传的图像插入 table)之外的所有东西都在工作。有人可以向我解释为什么它不起作用吗?或者,如果有另一种方法可行,我很想听听。下面的文件是我的upload.inc.php。我打算在将图像插入到我的 table.
后在另一个名为 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.";}
}
您正在对新图像进行 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();
}
$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);
除了 $sqlImg(应该将上传的图像插入 table)之外的所有东西都在工作。有人可以向我解释为什么它不起作用吗?或者,如果有另一种方法可行,我很想听听。下面的文件是我的upload.inc.php。我打算在将图像插入到我的 table.
后在另一个名为 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.";}
}
您正在对新图像进行 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();
}
$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);