1

whenever i tried to insert data with image to my database, image are not submitted or saved. it gives only like this [BLOB-0B] but the data (text) were sucessfully saved.

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>

sorry for long lines of codes, i hope you could help me.

Jeffrey
  • 1,239
  • 8
  • 15
  • 1
    *PSA:* The `mysql_*` functions are [deprecated in PHP 5.5](http://php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated). It is not recommended for writing new code as it will prevent you from upgrading in the future. Instead, use either [MySQLi](http://php.net/manual/en/book.mysqli.php) or [PDO](http://php.net/manual/en/book.pdo.php) and [be a better PHP Developer](http://jason.pureconcepts.net/2012/08/better-php-developer/). – Jason McCreary Sep 04 '13 at 14:55
  • 5
    Don't save the images into a database, the overload is humongous. Instead you should store the files on the server and save the file location and or file names. – Jonast92 Sep 04 '13 at 14:58
  • The usual: ``mysql_*`` functions are deprecated in PHP 5, and you should escape whatever you put into your queries. PDO is recommended. – Jeffrey Sep 04 '13 at 15:22

1 Answers1

0

First of all, I recommend to use the folowing INSERT INTO syntax, in-which absolute determination of every column value:

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

Second: Your form tag should has the following attribute enctype="multipart/form-data", to allow file uploading, so your form should be like:

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

The last thing you have to migrate mysql_ functions as other commenters said. You may checkout the tag and look at this question's answer to know why: Why shouldn't I use mysql_* functions in PHP?

Community
  • 1
  • 1
SaidbakR
  • 13,303
  • 20
  • 101
  • 195