1

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

外部.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();
    }

}

事件.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>

抱歉,代码很长,希望您能帮助我。

4

1 回答 1

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_ 函数的最后一件事。您可以检查标记并查看此问题的答案以了解原因: 为什么我不应该在 PHP 中使用 mysql_* 函数?

于 2013-09-04T15:17:38.737 回答