Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.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数据库-“;意外的$end“;错误_Php_Mysql - Fatal编程技术网

Php 表单未将图像插入MySQL数据库-“;意外的$end“;错误

Php 表单未将图像插入MySQL数据库-“;意外的$end“;错误,php,mysql,Php,Mysql,我正在尝试使用下面的表单向MySQL数据库提交4个图像。当我单击“上载图像”按钮时,会出现一个错误,在第235行显示语法错误,意外$end in…postsubmit.php。第235行没有任何内容;这只是postsmit.php上的最后一行。知道我为什么会犯这个错误吗 提前感谢, 约翰 表格: echo '<form method="post" action="postsubmit.php" enctype="multipart/form-data">

我正在尝试使用下面的表单向MySQL数据库提交4个图像。当我单击“上载图像”按钮时,会出现一个错误,在第235行显示
语法错误,意外$end in…postsubmit.php。第235行没有任何内容;这只是postsmit.php上的最后一行。知道我为什么会犯这个错误吗

提前感谢,

约翰

表格:

    echo '<form method="post" action="postsubmit.php" enctype="multipart/form-data">

          <input type="hidden" value="'.$_SESSION['loginid'].'" name="uid">

           <div class="imagetitle"><label for="image">Image 1:</label></div>
           <div class="imagefield"><input type="file" name="image" /></div>

           <div class="imagetitle2"><label for="image2">Image 2:</label></div>
           <div class="imagefield2"><input type="file" name="image2" /></div>

           <div class="imagetitle3"><label for="image3">Image 3:</label></div>
           <div class="imagefield3"><input type="file" name="image3" /></div>

           <div class="imagetitle4"><label for="image4">Image 4:</label></div>
           <div class="imagefield4"><input type="file" name="image4" /></div>

           <div class="submissionbutton"><input type="submit" value="Upload Image" /></div>



</form>';

通常,该错误来自PHP解析器在找到预期语法之前到达脚本末尾。很可能是未闭合的大括号
}

通常,错误来自PHP解析器在找到预期语法之前到达脚本末尾。很可能是一个未闭合的大括号
}

在文件的最后再添加一个大括号
}

您从未关闭过
assertValidUpload()
函数

通常应避免编写如此冗长的函数。尝试将其分解为多个函数--一般来说,函数只能完成一项任务

此外,您的代码样式不一致(可能只是此网站上的格式不一致):


assertValidUpload()
函数的开头,将大括号放在下一行,应将其移动到函数定义声明的同一行。这类事情有助于突出代码中的不一致性,从而帮助您注意到类似这样的错误。

在文件的最后再添加一个右括号
}

您从未关闭过
assertValidUpload()
函数

通常应避免编写如此冗长的函数。尝试将其分解为多个函数--一般来说,函数只能完成一项任务

此外,您的代码样式不一致(可能只是此网站上的格式不一致):


assertValidUpload()
函数的开头,将大括号放在下一行,应将其移动到函数定义声明的同一行。这类事情有助于突出代码中的不一致性,从而帮助您注意到类似这样的错误。

在文档末尾加上三个大括号(
}
),看看是否有其他错误。我建议您使用支持代码折叠或至少支持大括号突出显示的代码编辑器(将光标放在大括号之前或旁边,突出显示结束/开始大括号)。在文档末尾放置三个大括号/花括号(
}}
),查看是否有其他错误。我建议您使用支持代码折叠或至少支持大括号高亮显示的代码编辑器(将光标放在大括号之前或旁边会高亮显示关闭/打开大括号)。谢谢。。。事实上我少了3个花括号谢谢。。。事实上我少了3个花括号谢谢。。。你是对的。但是assertValidUpload()已关闭。4个问题中有3个没有结束。好吧,如果你按照我其余答案中的建议去做,你会很快注意到的。:)另外,尝试使用文本编辑器实现大括号、括号、方括号和引号的自动配对。那你就不用想了汉克斯。。。你是对的。但是assertValidUpload()已关闭。4个问题中有3个没有结束。好吧,如果你按照我其余答案中的建议去做,你会很快注意到的。:)另外,尝试使用文本编辑器实现大括号、括号、方括号和引号的自动配对。那你就不用想了
function assertValidUpload($code)
    {
        if ($code == UPLOAD_ERR_OK) {
            return;
        }

        switch ($code) {
            case UPLOAD_ERR_INI_SIZE:
            case UPLOAD_ERR_FORM_SIZE:
                $msg = 'Image is too large';
                break;

            case UPLOAD_ERR_PARTIAL:
                $msg = 'Image was only partially uploaded';
                break;

            case UPLOAD_ERR_NO_FILE:
                $msg = 'No image was uploaded';
                break;

            case UPLOAD_ERR_NO_TMP_DIR:
                $msg = 'Upload folder not found';
                break;

            case UPLOAD_ERR_CANT_WRITE:
                $msg = 'Unable to write uploaded file';
                break;

            case UPLOAD_ERR_EXTENSION:
                $msg = 'Upload failed due to extension';
                break;

            default:
                $msg = 'Unknown error';
        }

        throw new Exception($msg);
    }

    $errors = array();

    try {
        if (!array_key_exists('image', $_FILES)) {
            throw new Exception('Image not found in uploaded data');
        }

        $image = $_FILES['image'];

        // ensure the file was successfully uploaded
        assertValidUpload($image['error']);

        if (!is_uploaded_file($image['tmp_name'])) {
            throw new Exception('File is not an uploaded file');
        }

        $info = getImageSize($image['tmp_name']);

        if (!$info) {
            throw new Exception('File is not an image');
        }
    }
    catch (Exception $ex) {
        $errors[] = $ex->getMessage();
    }

    if (count($errors) == 0) {
        // no errors, so insert the image

        $query = sprintf(
            "insert into images (filename, mime_type, file_size, file_data)
                values ('%s', '%s', %d, '%s')",
            mysql_real_escape_string($image['name']),
            mysql_real_escape_string($info['mime']),
            $image['size'],
            mysql_real_escape_string(
                file_get_contents($image['tmp_name'])
            )
        );

        mysql_query($query);




    try {
        if (!array_key_exists('image2', $_FILES)) {
            throw new Exception('Image not found in uploaded data');
        }

        $image2 = $_FILES['image2'];

        // ensure the file was successfully uploaded
        assertValidUpload($image2['error']);

        if (!is_uploaded_file($image2['tmp_name'])) {
            throw new Exception('File is not an uploaded file');
        }

        $info2 = getImageSize($image2['tmp_name']);

        if (!$info2) {
            throw new Exception('File is not an image');
        }
    }
    catch (Exception $ex2) {
        $errors2[] = $ex2->getMessage();
    }

    if (count($errors2) == 0) {
        // no errors, so insert the image

        $query2 = sprintf(
            "insert into images (filename, mime_type, file_size, file_data)
                values ('%s', '%s', %d, '%s')",
            mysql_real_escape_string($image2['name']),
            mysql_real_escape_string($info2['mime']),
            $image2['size'],
            mysql_real_escape_string(
                file_get_contents($image2['tmp_name'])
            )
        );

        mysql_query($query2);       




    try {
        if (!array_key_exists('image3', $_FILES)) {
            throw new Exception('Image not found in uploaded data');
        }

        $image3 = $_FILES['image3'];

        // ensure the file was successfully uploaded
        assertValidUpload($image3['error']);

        if (!is_uploaded_file($image3['tmp_name'])) {
            throw new Exception('File is not an uploaded file');
        }

        $info3 = getImageSize($image3['tmp_name']);

        if (!$info3) {
            throw new Exception('File is not an image');
        }
    }
    catch (Exception $ex3) {
        $errors3[] = $ex3->getMessage();
    }

    if (count($errors3) == 0) {
        // no errors, so insert the image

        $query3 = sprintf(
            "insert into images (filename, mime_type, file_size, file_data)
                values ('%s', '%s', %d, '%s')",
            mysql_real_escape_string($image3['name']),
            mysql_real_escape_string($info3['mime']),
            $image3['size'],
            mysql_real_escape_string(
                file_get_contents($image3['tmp_name'])
            )
        );

        mysql_query($query3);       




    try {
        if (!array_key_exists('image4', $_FILES)) {
            throw new Exception('Image not found in uploaded data');
        }

        $image4 = $_FILES['image4'];

        // ensure the file was successfully uploaded
        assertValidUpload($image4['error']);

        if (!is_uploaded_file($image4['tmp_name'])) {
            throw new Exception('File is not an uploaded file');
        }

        $info4 = getImageSize($image4['tmp_name']);

        if (!$info4) {
            throw new Exception('File is not an image');
        }
    }
    catch (Exception $ex4) {
        $errors4[] = $ex4->getMessage();
    }

    if (count($errors4) == 0) {
        // no errors, so insert the image

        $query4 = sprintf(
            "insert into images (filename, mime_type, file_size, file_data)
                values ('%s', '%s', %d, '%s')",
            mysql_real_escape_string($image4['name']),
            mysql_real_escape_string($info4['mime']),
            $image4['size'],
            mysql_real_escape_string(
                file_get_contents($image4['tmp_name'])
            )
        );

        mysql_query($query4);               




        header("Location: http://www...com/.../");
        exit;
    }