我正在尝试使用 php 将一些信息上传到数据库,我已经能够单独上传图像和文本,但不能一起上传(我的目标)。由于某种原因,图像将进入指定的文件夹,但是由于某种原因,信息不会被放入数据库,并且不断向我抛出“失败”的 else 语句!我显然是 php 的新手,我们将不胜感激!这是我的 PHP 代码:
<?php
include_once 'dbh.inc.php';
session_start();
if(isset($_POST['upload'])) {
$file = $_FILES['profileup'];
$fileName = $_FILES['profileup']['name'];
$fileTmpName = $_FILES['profileup']['tmp_name'];
$fileSize = $_FILES['profileup']['size'];
$fileError = $_FILES['profileup']['error'];
$fileType = $_FILES['profileup']['type'];
$Newname = $fileName;
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png');
if (in_array($fileActualExt, $allowed)){
if ($fileError === 0){
if ($fileSize < 10000000000){
$filedest = 'postimages/'.$Newname;
$username = $_SESSION[user];
move_uploaded_file($fileTmpName, $filedest);
header("Location: ../UserProfile.php?suc");
$sql = "INSERT INTO posts (img, ext) VALUES ('$filedest', '$fileActualExt');";
$result = mysqli_query($conn, $sql);
if($result !== false) {
exit;
}else{
echo "fail";
}
}else{
echo "your file was to big";
}
}else{
echo "There was an error uploading your file";
}
}else{
echo "you cant upload files of this type";
}
}else{
header("Location: ../UserProfileSetting.php?fail");
}
?>
最佳答案
Move your redirection part after you complete your insert query.
The code below will do your trick.
The problem is you r redirecting your page before executing your insert query.
if ($fileSize < 10000000000){
$filedest = 'postimages/'.$Newname;
$username = $_SESSION[user];
move_uploaded_file($fileTmpName, $filedest);
$sql = "INSERT INTO posts (img, ext) VALUES ('$filedest', '$fileActualExt');";
$result = mysqli_query($conn, $sql);
if($result !== false) {
header("Location: ../UserProfile.php?suc");
}else{
echo "fail";
}
}else{
echo "your file was to big";
}
关于php图像和文本上传不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50013427/