在php mysql中插入和检索图像

时间:2013-09-04 14:50:11

标签: php html mysql

每当我尝试将带有图像的数据插入我的数据库时,都不会提交或保存图像。它只给出了这样的[BLOB-0B],但数据(文本)已成功保存。

external.php

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

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

对不起长长的代码,我希望你能帮助我。

1 个答案:

答案 0 :(得分:0)

首先,我建议使用以下INSERT INTO语法,其中绝对确定每列值:

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

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

<form name="eventform" id="eventform" method="post" action="events.php" enctype="multipart/form-data">
  

您必须将mysql_函数迁移到其他评论者的最后一件事   说过。您可以查看标记并查看此问题   回答知道原因:   Why shouldn't I use mysql_* functions in PHP?