首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在php mysql中插入和检索图像

在php mysql中插入和检索图像
EN

Stack Overflow用户
提问于 2013-09-04 14:50:10
回答 1查看 468关注 0票数 1

每当我试图将带有图像的数据插入到数据库中时,都不会提交或保存图像。它只给出了类似于这个BLOB-0B,但数据(文本)保存得很成功。

external.php

代码语言:javascript
运行
复制
class Dbother {

    private $host = 'localhost';
    private $user = 'root';
    private $password = '';
    private $dbname = 'pcnl';
    private $conn = '';

    function connect() {
        $this->conn = mysql_connect($this->host, $this->user, $this->password) or die (mysql_error());
        mysql_select_db($this->dbname, $this->conn) or die(mysql_error());
    }

    function addEvent($title, $place, $date, $people, $content, $eventImg, $EventExt) {
        $sql = "insert into tbl_event values('$title', '$place', '$date', '$people', '$content', null, null, '$eventImg', '$EventExt', null)";
        mysql_query($sql) or die (mysql_error());
        mysql_close();
    }

}

event.php

代码语言:javascript
运行
复制
<?php
require_once 'dbother.php';
$object = new Dbother();
$object->connect();
if(isset($_POST['title'])){
            $title = $_POST['title'];
            $place = $_POST['place'];
            $date = $_POST['date'];
            $people = $_POST['people'];
            $content = $_POST['content'];

            $eventImg=file_get_contents($_FILES['pic']['tmp_name']);
            $eventImg=mysql_escape_string($eventImg);

            @list(, , $imtype, ) = getimagesize($_FILES['pic']['tmp_name']);

            if ($imtype == 3){
                $EventExt="png"; 
            }elseif ($imtype == 2){
                $EventExt="jpeg";
            }elseif ($imtype == 1){
                $EventExt="gif";
            }

            $object->addEvent($title, $place, $date, $people, $content, $eventImg, $EventExt);
            header('Location: events.php');
            exit;
            }
    ?>
        <div>
        <h4>New Event</h4>
        <form name="eventform" id="eventform" method="post" action="events.php">
        <p> Title: <input type="text" name="title" id="title" required pattern="[A-Za-z0-9 ,.)(*!?|<>:]{5,80}" title="Please enter valid title of you event."/></p>
        <p> Where:  <input type="text" name="place" id="place" required pattern="[A-Za-z0-9 ,.)(*!?|<>:]{5,100}"/></p>
        <p>When:  <input type="date" name="date" id="date" width="40px;" required /></p>
        <p>People Involved: <input type="text" name="people" id="people" required/></p>
        <p>Content:</p>
        <textarea rows="4" cols="50" required name="content"></textarea>
        <p><input type="file" name="pic" id="file" style="float:left">
        <a href ="events.php"><input name="btnexit" type="button" id="btnexit" value="Cancel" style="width:80px; height:30px; margin:5px; border:2px solid #757575; font-family:'Trebuchet MS', Arial, Helvetica, sans-serif; float:right"></a>
        <input name="btnfinish" type="submit" class="btnfinish" value="Done" style="width:80px; height:30px; margin:5px; border:2px solid #757575; font-family:'Trebuchet MS', Arial, Helvetica, sans-serif; float:right"></p>
        </form>

抱歉,密码排得太长了,希望你能帮我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-04 15:17:38

首先,我建议使用折叠INSERT INTO语法,其中绝对确定每个列的值:

代码语言:javascript
运行
复制
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

第二:表单标记应该具有以下属性enctype="multipart/form-data",以允许文件上载,因此表单应该如下所示:

代码语言:javascript
运行
复制
<form name="eventform" id="eventform" method="post" action="events.php" enctype="multipart/form-data">

正如其他评论者所说,您必须迁移mysql_函数。您可以查看mysql标记并查看这个问题的答案,以了解为什么:PHP中的函数?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18617052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档