php图片和文本上传不工作

n6lpvg4x  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(326)

我试图上传一些信息到数据库使用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");
    }    
?>
zour9fqk

zour9fqk1#

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";
                            }

相关问题