0

I know it is not a good idea to store images in mysql database but I just wanted to try It.

in mysql I created this table:

CREATE TABLE tbl_images (
      id tinyint(3) unsigned NOT NULL auto_increment,
      image mediumblob NOT NULL,
      PRIMARY KEY (id)
      );

and here is the php code:

 if(isset($_FILES['image']) && $_FILES['image']['size'] > 0) {
$tmpName  = $_FILES['image']['tmp_name'];

      $fp   = fopen($tmpName, 'r');
      $data = fread($fp, filesize($tmpName));
      $data = addslashes($data);
      fclose($fp);

      $query ="INSERT INTO tbl_images(image)VALUES('".$data."')";
      $results=mysql_query($query) or die(mysql_error());
       $num=mysql_num_rows($results);
       if($num>0)
           print "Thank you, your file has been uploaded.";
}
else {
   print "No image selected/uploaded";
}
?>

I have error in sytax of mysql here is the output:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in ...\insertimg.php on line 21

<html>
<title>Image</title>
<!--mikhaim ie form me3 upload ghabli benevisim-->
<!-- inbar mikhaim 2 database image ra gharar dahim-->
<form enctype="multipart/form-data" action="insertimg.php" method="post" name="changer">
    <input name="MAX_FILE_SIZE" value="102400" type="hidden">
    <input name="image" id="image" accept="image/jpeg" type="file">
    <input value="Submit" type="submit">
    </form>
    </html>

3 Answers 3

1

Your Else bracket is not closed .....

Sign up to request clarification or add additional context in comments.

2 Comments

I don't know how! but problem solved I used head in the html form but I think something else was a problem which I don't know it
if(isset($_FILES['image']) && $_FILES['image']['size'] > 0) this condition was always false.... it can be for different reasons ....
0

There's no point in checking num_rows on an INSERT query. If you want to print errors when they occur simply do: mysql_query(...) or die(mysql_error()) instead.

Edit: I can't really see why your INSERT query would ever fail anyway so you can remove all the error checking and just print "Thanks" after the query.

Edit: Here's what it should look like:

<?php
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {
    $tmpName = $_FILES['image']['tmp_name'];
    $fp = fopen($tmpName, 'r');
    $data = fread($fp, filesize($tmpName));
    $data = addslashes($data);
    fclose($fp);

    $query = "INSERT INTO tbl_images (image) VALUES ('$data')";
    mysql_query($query) or die(mysql_error());
    print "Thank you, your file has been uploaded.";
else {
    print "No image selected/uploaded";
}

Comments

0

Make sure record is inserting, if its inserting fine then you can use

$num = msql_affected_rows(); 

or

$num = mysql_insert_id();

instead of $num=mysql_num_rows($results);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.