PHP MySQL查询以串联分配变量进行插入

时间:2018-11-17 15:14:27

标签: php mysql sql

我正面临php插入多个图像和唯一的帖子ID到数据库的问题。 在数据库中,我有图像表和发布表,当我上传多个图像时,图像将存储在具有当前发布ID的图像表中。

问题是插入语法错误,因为

  

$ insertValuesSQL

是通过使用.=串联分配来上传图像的。

对我来说,这是添加容易添加的帖子ID,以便我从其中获取图像,因此,我必须插入两件事,分别是$insertValuesSQL$last_id帖子ID。

有人可以帮助我纠正语法并能够上传带有帖子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");

成功上传多张图片,但没有图片的帖子ID

引用自: https://www.codexworld.com/upload-multiple-images-store-in-database-php-mysql/

1 个答案:

答案 0 :(得分:2)

您需要将$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");
相关问题