mysql查询

xwbd5t1u  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(166)

我面临着php插入多个图像和数据库中唯一post id的问题。在数据库中我有图像表和post表,当我上传多个图像时,图像将以当前post id存储在图像表中。
问题是insert语法错误,因为
$insertvaluessql插入值
是从为每个使用 .= 用于上载图像的串联分配。
问题是我要添加额外的post id,这样我就可以很容易地从哪个post id得到图像,所以我必须插入两个 $insertValuesSQL 以及 $last_id 从post id。
有人能帮我纠正语法,并能够上传与post-id的图像吗?谢谢你。
错误位置:

$insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL,$last_id");

php完整代码:

$targetDir = "uploads/";
$allowTypes = array('jpg','png','jpeg','gif');

$statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';
if(!empty(array_filter($_FILES['submit-image']['name']))){
    foreach($_FILES['submit-image']['name'] as $key=>$val){
        // File upload path
        $fileName = basename($_FILES['submit-image']['name'][$key]);
        $targetFilePath = $targetDir . $fileName;

        // Check whether file type is valid
        $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);

        if(in_array($fileType, $allowTypes)){
            // Upload file to server
            if(move_uploaded_file($_FILES["submit-image"]["tmp_name"][$key], $targetFilePath)){
                // Image db insert sql
                $insertValuesSQL .= "('".$fileName."'),";
            }else{
                $errorUpload .= $_FILES['submit-image']['name'][$key].', ';
            }
        }else{
            $errorUploadType .= $_FILES['submit-image']['name'][$key].', ';
        }
    }

    if (mysqli_query($conn, $sql)) {
        $last_id = mysqli_insert_id($conn);
        echo "New record created successfully. Last inserted ID is: " . $last_id;
        if(!empty($insertValuesSQL)){
            $insertValuesSQL = trim($insertValuesSQL,',');
            // Insert image file name into database
            $insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL,$last_id");
            if($insert){
                $errorUpload = !empty($errorUpload)?'Upload Error: '.$errorUpload:'';
                $errorUploadType = !empty($errorUploadType)?'File Type Error: '.$errorUploadType:'';
                $errorMsg = !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType;
                $statusMsg = "Files are uploaded successfully.".$errorMsg;
            }else{
                $statusMsg = "Sorry, there was an error uploading your file.";
            }
        }
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }

}else{
    $statusMsg = 'Please select a file to upload.';
}

再加一个
如果我使用以下代码:

$insert = $conn->query("INSERT INTO test (file_name) VALUES $insertValuesSQL");

上载多个图像成功,但没有图像的post id
参考来源:https://www.codexworld.com/upload-multiple-images-store-in-database-php-mysql/

cmssoen2

cmssoen21#

你需要把 $last_id 插入到每个值列表中,而不是作为单独的参数。但你不能这么做因为你在创造 $insertValuesSQL 在你出发之前 $last_id .
可以移动创建 $insertValuesSQL 在你准备好之后 $last_id ,但另一种方法是使用mysql的内置函数 LAST_INSERT_ID() :

$insertValuesSQL .= "('".$fileName."', LAST_INSERT_ID()),";

然后你就可以 $last_id 从后面出来 INSERT 查询:

$insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL");

相关问题