上传多个图像时发生错误,表示数据无法更新

nhn9ugyo  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(527)

我试图使多个图像上传与数据连接,所以图像名称应保存在数据库中,现在当我上传图像,它是上传,也显示在文件夹中,但没有在我的数据库中上传,并说对不起,数据无法更新!
请帮帮我,我已经试了一天了。这是我的上传代码

<?php
require_once 'dbconfig.php';
require_once 'common_functions.php';

if (isset($_POST["sub"])) {
    $msg = "";
    if (count($_FILES["user_files"]) > 0) {
        $folderName = "uploads/";
        $counter = 0;

        for ($i = 0; $i < count($_FILES["user_files"]["name"]); $i++) {

            $file_name=$_FILES["user_files"]["name"][$i];

            if ($_FILES["user_files"]["name"][$i] <> "") {
                $ext = strtolower(pathinfo($file_name,PATHINFO_EXTENSION)); // get image extension
                $file_name = $folderName . rand(10000, 990000) . '_' . time() . '.' . $ext;
                $valid_extensions = array('jpeg', 'jpg', 'png'); // valid extensions
                if(in_array($ext, $valid_extensions)){  
                    $filename=basename($file_name,$ext);
                    $newFileName=$filename.$ext;
                    if (!move_uploaded_file($_FILES["user_files"]["tmp_name"][$i],"Uploads/".$newFileName)) {
                        $msg .= "Failed to upload" . $_FILES["user_files"]["name"][$i] . ". <br>";
                        $counter++;
                    }
                    if(!isset($msg)) {
                        $stmt = $DB_con->prepare('INSERT INTO products SET productname=:productname');
                        $stmt->bindParam(':productname',$newFileName);
                        if($stmt->execute()){

                            $msg = "Your Post ' ".$newFileName." ' has been successfully uploaded , <a  target='_blank'   href='post.php?name=".$newFileName." '>View</a>.";

                            // redirects video view page after 5 seconds.
                        } else {
                            $msg = "error while inserting....";
                        }
                    } else {
                        $msg = "Sorry Data Could Not Updated !";
                    }
                } else {
                    $msg = "Sorry, only JPG, JPEG, PNG  files are allowed.";        
                }
            }
        }
    }

} else {
    $msg = errorMessage("You must upload atleast one file");
}
?>
wqsoz72f

wqsoz72f1#

“insert”语句中没有“set”。尝试。。。

'INSERT INTO products (productname) VALUES (:productname)'

将来,您可以通过触发mysqli错误并显示以下内容来节省一天左右的时间:

$stmt->execute() or trigger_error($stmt->error, E_USER_ERROR);

相关问题