I have a simple form to select a file and insert it into a blob field. However, its not getting inserted into the DB. No error messages.

Php 4, apache 2, mysql 3.23

<?php require_once('Connections/rbc.php'); ?>
<?
If($Picture != "") {
$PSize = filesize($Picture);
$mysqlPicture = addslashes(fread(fopen($Picture, "r"), $PSize));
mysql_query("INSERT INTO Images (Image) VALUES ('$mysqlPicture')") or die("Can't Perform Query");
}
else {
echo"You did not upload any picture";
}
?>

Any ideas?

TIA

    4 months later

    If($Picture != "") {

    does $Picture exist ? use $POST or $GET

    $mysqlPicture = addslashes(fread(fopen($Picture, "r"), $PSize));

    Well I am not sure you can call 3 functions on the same line. Moreover it's unreadable for the mere mortal πŸ™‚

    JM

      I asked this long ago, but here goes again. Why is everyone making their lives exponentially more complicated trying to cram large files into a database? Why not just store the file in the filesystem and reference it in the db? It would keep your db size down and it's a lot less painful.

        8 months later

        $DB = "kemal";
        $link = mysql_connect("localhost","root","") or die("Can't connect");
        $sel = mysql_select_db($D😎 or die("Can't select $DB");

        $fileinput = "C:\Documents and Settings\kmemis\My Documents\My Pictures\Sample.jpg";
        $image = @addslashes(fread(fopen($fileinput,"r"), 1000000));
        $SQL = @"INSERT INTO tablename (no,body) values (null, \"" . $image . "\")";
        $Result = mysql_query($SQL,$link);

        I have written this code and result is different from "0"!so it must have been added to my database!when Γ½ look at it in mysql-front record was added but Γ½ can't see the image in blob editor..πŸ™please help me...

          $fileinput = "C:\Documents and Settings\kmemis\My Documents\My Pictures\Sample.jpg";

          Try a local file, stored near your PHP script :
          MyProject\myphpscript.php
          MyProject\images\Sample.jpg

          You need reading rights to the folder. I'm not sure your access rights are properly set for the My Documents folder.

          $image = @addslashes(fread(fopen($fileinput,"r"), 1000000));

          Read previous remarks. It's unreadable, NEVER use 3 function calls on a line. You can't debug. fopen may fails, fread may fails... Moreover you actually hide all errors with @!

          $SQL = @"INSERT INTO tablename (no,body) values (null, \"" . $image . "\")";

          @ to a " ? Read the manual for more info about the @ operator. Use it before function, it should be before mysql_query, not the query string.

          fread(fopen($fileinput,"r"), 1000000)

          Use file () to read a whole file. This way you don't need to specify that "stupid" number of bytes thing.

          $SQL = @"INSERT INTO tablename (no,body) values (null, \"" . $image . "\")";

          Replace \" by ', it's easier to debug and read.

          Read the whole post again and pay attention to our remarks, specially the one by
          jerdo !

          Hope it helps πŸ™‚
          JM

            thanks a lot!πŸ™‚ when I copied the picture in the image folder,I can merge it into data base!I can see it in the blob editor!but this time
            Γ½ can't see it in my php window although Γ½ wrote these codes...
            $DB = "kemal";
            $link = mysql_connect("localhost","root","") or die("Can't connect");
            $sel = mysql_select_db($D😎 or die("Can't select $DB");
            $query = "SELECT body FROM tablename WHERE no=261";

            if(!($result = @ mysql_query ($query, $link)))
            {echo("unsuccessful..."); }

            $data = @ mysql_fetch_array($result);

            if (!empty($data['body']))
            {
            //This content type header should be whatever type your image is
            header("Content-Type: image/jpg");
            echo $data['body'] ;
            gives me warning in header line....and shows me the text version of the picture.....

              Write a Reply...