Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP MySQL查询带串联赋值变量的insert_Php_Mysql_Sql - Fatal编程技术网

PHP MySQL查询带串联赋值变量的insert

PHP MySQL查询带串联赋值变量的insert,php,mysql,sql,Php,Mysql,Sql,我遇到了php插入多个图像和数据库中唯一的post id的问题。 在数据库中,我有图像表和post表,当我上传多个图像时,图像将以当前post id存储在图像表中 问题是插入语法错误,因为 $insertValuesSQL 使用=串联分配上载图像时,每个都从 这是因为我需要添加额外的post id,这样我就可以很容易地从中获取post id的图像,因此我必须插入两个内容,即post id中的$insertValuesSQL和$last\u id 有人能帮我纠正语法,并能上传带有帖子id的图片吗?

我遇到了php插入多个图像和数据库中唯一的post id的问题。 在数据库中,我有图像表和post表,当我上传多个图像时,图像将以当前post id存储在图像表中

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

$insertValuesSQL

使用
=
串联分配上载图像时,每个都从

这是因为我需要添加额外的post id,这样我就可以很容易地从中获取post id的图像,因此我必须插入两个内容,即post id中的
$insertValuesSQL
$last\u id

有人能帮我纠正语法,并能上传带有帖子id的图片吗?A谢谢你

错误位于:

 $insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL,$last_id");
$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.';
}
PHP完整代码:

 $insert = $conn->query("INSERT INTO test (file_name, post_id) VALUES $insertValuesSQL,$last_id");
$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.';
}
上载多个图像成功,但该图像没有post id

参考来源:

您需要将
$last\u id
放入插入的每个值列表中,而不是作为单独的参数。但您不能这样做,因为您在设置
$last\u id
之前正在创建
$insertValuesSQL

设置
$last\u id
后,可以将创建
$insertValuesSQL
的循环移动到,但另一种方法是使用MySQL的内置函数
last\u INSERT\u id()

然后,您可以从后面的
插入
查询中提取
$last\u id

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

您需要将
$last\u id
放入插入的每个值列表中,而不是作为单独的参数。但您不能这样做,因为您在设置
$last\u id
之前正在创建
$insertValuesSQL

设置
$last\u id
后,可以将创建
$insertValuesSQL
的循环移动到,但另一种方法是使用MySQL的内置函数
last\u INSERT\u id()

然后,您可以从后面的
插入
查询中提取
$last\u id

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

@user3783243是的,这是一个insert post查询,所以在成功插入post之后,我得到了上传图像的最后一个id,idOutput实际的查询,看起来它将被破坏。我认为它会变成
插入测试(文件名,asd)值('filename')、最后一个id值
,这是行不通的。使用
mysqli
错误报告功能,获得实际错误。这也对SQL注入开放。我建议对参数化查询执行此操作。@user3783243抱歉,我的错误,我键入了错误的
test(file\u name,asd)
这易受SQL注入攻击。不要忽略使用参数化查询的建议。@user3783243是的,这是一个insert post查询,因此在成功插入post后,我用idOutput获取上载图像的最后一个id。实际查询的结果看起来会非常糟糕。我认为它会变成
插入测试(文件名,asd)值('filename')、最后一个id值
,这是行不通的。使用
mysqli
错误报告功能,获得实际错误。这也对SQL注入开放。我建议对参数化查询执行此操作。@user3783243抱歉,我的错误,我键入了错误的
test(file\u name,asd)
这易受SQL注入攻击。不要忽略使用参数化查询的建议。
LAST\u INSERT\u ID()
可以创建竞争条件,不是吗?不,它与
mysqli\u INSERT\u ID()完全相同。
。它们都获得当前连接插入的最后一个ID。
last\u INSERT\u ID()
可以创建竞争条件,不是吗?不,它与
mysqli\u INSERT\u ID()完全相同。
。它们都获得当前连接插入的最后一个ID。