Jump to content

Unable to upload image to a database.


cool_techie

Recommended Posts

I am unable to upload image to db,

I get a message "No image selected/uploaded"  after every trial,

 

My HTML form is:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form enctype="multipart/form-data" action="insert.php" method="post">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
</form>
</body>
</html>

and my php script "insert.php" is-

<?php
// Create MySQL login values and 
// set them to your login information.
$username = "root";
$password = "";
$host = "localhost";
$database = "test";

// Make the connect to MySQL or die
// and display an error.
$link = mysql_connect($host, $username, $password);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

// Select your database
mysql_select_db ($database); 

// Make sure the user actually 
// selected and uploaded a file
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

      // Temporary file name stored on the server
      $tmpName  = $_FILES['image']['tmp_name'];  
       
      // Read the file 
      $fp      = fopen($tmpName, 'r');
      $data = fread($fp, filesize($tmpName));
      $data = addslashes($data);
      fclose($fp);
      

      // Create the query and insert
      // into our database.
      $query = "INSERT INTO tbl_images ";
      $query .= "(image) VALUES ('$data')";
      $results = mysql_query($query, $link);
      
      // Print results
      print "Thank you, your file has been uploaded.";
      
}else{
   print "No image selected/uploaded";
}

// Close our MySQL Link
mysql_close($link);
?>  


my table consists if an "id" and an "image" of type "blob".

 

 

Link to comment
Share on other sites

My advice is "don't put images into a database. Save and use them from a file. Put the image name into the database." But if you really want to do this:

 

I would first find out if I'm uploading the image. Put it into a folder and see if that is working.

 

Then go here and read the last entry "MySQL Image Table" ->  http://hockinson.com/

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.