当前位置: 首页 > 知识库问答 >
问题:

在数据库中插入图片

徐兴昌
2023-03-14

几个月前,我开始编写我的第一个动态页面...从零开始学习前端和后端的一切。

这里和那里都有一些颠簸,所以这是又一个。

这是一个房地产页面,人们可以在其中添加物品和图片。我读了一些关于数据库中图片问题的著名文章。我只是决定使用DB,因为我需要从某个地方开始。

我用这个教程开始。几个小时后,瞧,我能够在一个真正的服务器中插入图片。非常高兴。

但问题来了。图片需要附加到用户名(或电子邮件)上。当您尝试插入内容时,该站点具有受用户名保护的页面。

这是将PIC插入数据库的主文件。注意,我在代码中添加了用户名,但是没有插入用户名,只是插入了图片。我需要知道谁在插入什么。

有一个附带的问题,你可以跳过:图片需要附在地址上。当用户选择图片并按下上传按钮时,我是否可以有一个带有地址的输入字段,在该字段中,同一个按钮将同时执行两个功能?(如果我有两个按钮,用户可能不会两个都做。这样,我们可以同时获得图片和地址,并在后面获得用户名)。我知道如何有两个按钮,两个查询。

非常感谢您的帮助。我真的需要它。

  $query = sprintf(
        "insert into images (filename, mime_type, file_size, file_data, username)
            values ('%s', '%s', %d, '%s','%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_real_escape_string(
            file_get_contents($_SESSION['user'])
        )
    );

这是完整的代码。

<?php
//error_reporting(E_ALL);
//ini_set("display_errors", 1);
require("common.php");   
 if(empty($_SESSION['user']))      
    {
       header("Location: ../index.php"); 
        die("Redirecting to .../index.php"); 
    }

?>

上面代码的第一部分只是检查用户是否登录。然后是代码。

<?php
    require_once('globalsConfigPic1.php');

    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, username)
            values ('%s', '%s', %d, '%s','%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_real_escape_string(
            file_get_contents($_SESSION['user'])
        )
    );

        mysql_query($query, $db);

        $id = (int) mysql_insert_id($db);

        // finally, redirect the user to view the new image
        header('Location: view.php?id=' . $id);
        exit;
    }
?>
<html>
    <head>
        <title>Error</title>
    </head>
    <body>
        <div>
            <p>
                The following errors occurred:
            </p>

            <ul>
                <?php foreach ($errors as $error) { ?>
                    <li>
                        <?php echo htmlSpecialChars($error) ?>
                    </li>
                <?php } ?>
            </ul>

            <p>
                <a href="upload.php">Try again</a>
            </p>
        </div>
    </body>
</html>

共有1个答案

燕志学
2023-03-14

是的。!以下是第一部分的部分解决方案:

$query = sprintf(
            "insert into images (filename, mime_type, file_size, file_data, username)
                values ('%s', '%s', %d, '%s','%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_real_escape_string  (
                 htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8')
            )
        );

现在正在处理与地址相连的图片。

 类似资料:
  • 我想我在这段代码中遇到了一点问题:当我试图在数据库中插入值时,我遇到了一个错误。

  • 我想在Jaspersoft Studio中的报表中的详细信息带中插入来自数据库的图像。 JPG图像保存在MySQL中的longblob类型的imgdata字段中。 我试图将此表达式放在Image元素中: 在第一种情况下,我得到以下错误: ...在第二种情况下,这个错误是: 我的问题是:如何将数据库中的图像插入到JasperReports的报表中?

  • 我正在尝试在数据库中插入乌尔都语文本,但只有“?????”插入其中。但在本地主机上工作得很好。当我在Cent-OS服务器上部署war时,在远程服务器上面临的问题。MySQL server 5.5.15正在Cent-OS服务器上运行。

  • 问题内容: 从Actor中读取的文件:Zac Efron 传记:他出生于加利福尼亚州的圣路易斯·奥比斯波,并在阿罗约·格兰德附近长大。在几集《夏姆兰》(2004)的客串中,他以女孩疯狂的卡梅隆·贝尔的身份参加常规演出。埃夫隆还出演过许多飞行员,例如卡尔·兰克(Carl Laemke)的《大世界》(2003)(电视)和三重播放(2004)(TV)。 More_Bio:Efron于2006年6月毕业于

  • 利用INSERT语句将数据插入表中 数据插入 用来插入(添加)行到数据库。 插入完整的行 插入行的一部分 插入某些查询结果 插入完整的行 指定表名和被插入到新行中的值 编写依赖与特定列次序的SQL语句,这样做有时会出错,但编写方便。 mysql> INSERT INTO Customers -> VALUES('1000000006', -> 'Toy Land', ->

  • 嗨,我是拉威尔的新手,我在将数据插入数据库和抛出错误时遇到了麻烦 msgstr"Countable():参数必须是实现可数的数组或对象" 我想在数据库中添加所有注册员工的出勤信息 控制器 叶片输出: 模型类员工出席扩展模型{// } 模型2 命名空间App; 使用Illumb\Database\Elount\Model; 类employee_data扩展模型{//protected$fillabe