php - 使用 php 在 mysql 中插入带有标题和详细信息的照片

标签 php html mysql

我制作了一个可以上传图像文件的页面,但我希望能够添加一些带有图片的详细信息并同时上传,我不知道如何从带有图片的文本框中上传详细信息到我的 table 上。

这是我的index.php代码:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="fa-IR">
<head>
<meta name="author" content="www.hamyarprojects.com" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Upload file</title>
<link href="css/Style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="mainframe" > 
    <div id="lp" class = "panel panel-primary">
        <div class = "panel-heading">
            <h3 class = "panel-title">Upload file test</h3>
        </div>
        <div class = "panel-body">
            <form action="Upload.php" method="post" enctype="multipart/form-data">
                <p id="btn_box">
                 <input type="file" name="upload_image" />
                </p>
                <p id="btn_box">
                <input type="submit" value="Upload Photo" name="submit">
                <input type="reset" value="Clear" name="reset">
                </p> 
            </form>
        </div>
    </div>
</div>
</body>
</html>

这是upload.php代码

<link href="css/Style.css" rel="stylesheet" type="text/css">
<div class="main">
<?php

if (!isset($_FILES['upload_image'])) {
    echo '<p>Please select a image</p>';
} else {
    try {
        upload();
        echo '<p>Upload successfully</p>';
    } catch(Exception $e) {
        echo '<h4>'.$e->getMessage().'</h4>';
    }
}

function upload() {
    if (is_uploaded_file($_FILES['upload_image']['tmp_name'])
        && getimagesize($_FILES['upload_image']['tmp_name']) != false
    ) {
        $size = getimagesize($_FILES['upload_image']['tmp_name']);
        $type = $size['mime'];
        $imgfp = fopen($_FILES['upload_image']['tmp_name'], 'rb');
        $size = $size[3];
        $name = $_FILES['upload_image']['name'];
        $maxsize = 99999999;

        if ($_FILES['upload_image']['size'] < $maxsize ) {
            $dbh = new PDO("mysql:host=localhost;dbname=bmc", 'root', '');
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            $stmt = $dbh->prepare("INSERT INTO imageblob (image_type, "
                . "image, image_size, image_name) VALUES (? ,?, ?, ?)");

            $stmt->bindParam(1, $type);
            $stmt->bindParam(2, $imgfp, PDO::PARAM_LOB);
            $stmt->bindParam(3, $size);
            $stmt->bindParam(4, $name);
            $stmt->execute();
        } else {
            throw new Exception("size invalid");
        }
    } else {
        throw new Exception("invalid format !");
    }
}

</div>

最佳答案

你能不能只添加一个文本框

<input type="text" name="imageDetails" />

然后在代码中捕获该文本框

$imageDetails = filter_input(INPUT_POST, "imageDetails", FILTER_SANITIZE_SPECIAL_CHARS);

然后向表 imageBlob 添加一列,名为 imageDetails

然后将您的查询更改为

$stmt = $dbh->prepare("INSERT INTO imageblob (image_type ,image, image_size, image_name, imageDetails) VALUES (? ,?, ?, ?, ?)");

然后添加绑定(bind)参数

$stmt->bindParam(5, $imageDetails);

希望这有帮助

关于php - 使用 php 在 mysql 中插入带有标题和详细信息的照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39796923/

相关文章:

html - 类中的伪元素未按预期工作

html - 调整窗口大小时的导航栏和 Div 排列(Bootstrap 响应)

mysql - 具有 2 个主键的内连接

php - 优化 MySQL 查询,使用子查询; MariaDB 快得多

php - Yii 命令行不自动加载模型

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

php - 发布三叶草覆盖率报告失败

javascript - 仅使用 Javascript(无服务器端)将文本区域内容下载为文件

sql - 在 MySQL 中获取累积和的最佳查询

php - Zend 框架错误 : Failed opening required 'Zend/Application.php'