我试图使用php将一些信息上传到数据库,我已经能够单独上传图像和文本但不能一起上传(我的目标)。由于某种原因,图像将进入指定的文件夹,但由于某种原因信息不会被放入数据库,并不断向我扔掉"失败"别的声明!我显然是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");
}
?>
&#13;
答案 0 :(得分:0)
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";
}