Nickool
Nickool

Reputation: 3702

syntax mysql/php error images

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>

Upvotes: 0

Views: 103

Answers (3)

fmask
fmask

Reputation: 481

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);

Upvotes: 0

Shaun
Shaun

Reputation: 2311

Your Else bracket is not closed .....

Upvotes: 1

powerbuoy
powerbuoy

Reputation: 12838

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";
}

Upvotes: 0

Related Questions