doushun1904 2016-09-30 17:27
浏览 31
已采纳

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

I made a page which I can upload an image file but I want to be able to add some details with picture and upload it at the same time and I dont have any idea how can I upload details from a text box with picture in to my table.

Here is my index.php code:

<!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>

and here is upload.php code

<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>
  • 写回答

1条回答 默认 最新

  • dovs36921 2016-09-30 19:29
    关注

    Could you not just add a text box

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

    Then in your code capture that text box

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

    Then add a column to your table imageBlob called imageDetails

    Then change your query to

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

    Then add the bind param

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

    Hopefully this helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染