Hi I have to build the like of a commercial website for my project and all was going well until I had to try to insert images in a SQL database ; I tried to modify some code that I found online but it is not working.
The idea is to use a form to upload the image but when I click on the submit button on my form the script does nothing it just give me a blank page:

insert.php:

<?php
// Create MySQL login values and 
// set them to your login information.
$username = "root";
$password = "I made sure that my pass is correct";
$host = "localhost";
$database = "Binary";

// 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["File"])) 
{ 
// Temporary file name stored on the server
$tmpName  = $_FILES["File"]["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 .= "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);
?>

Can someone please help me to get this to work?I badly need that for my project and I am not a php expert at all.

Recommended Answers

All 5 Replies

Instead of save image in database always save path of that image into DB.
and to show image call path like

<img src="<? echo path from DB ?>">

If you want to save image into DB read the http://www.mwasif.com/2007/4/save-image-in-mysql-with-php/

Okay I got my images in an sql database and here ia a script to display the image that is stored in the database and this works:

<?php
   //variables that will be needed to connect to mysql
  $host="localhost";
  $username="root";
  $password="xxxxxx";
  $database="binary";
  //make connection to mysql
  //and store connection in the variale $con
  $con=mysql_connect($host,$username,$password);
  //die if it didn't connect
  if(!$con)
  {
  die('Could not connect'. mysqlerror());
  }
  //if connected
  //select database to use
  mysql_select_db($database);
  //store query in variable $query
  $query="SELECT image from tbl_images where id=1";
  $results=mysql_query($query,$con);
  $row = mysql_fetch_array($results);
  echo $row['image'];
?>

I managed to get the image to display on a html page but the problem I am getting is that if I want display say 20 images from my database I will have to write 20 scripts.
Can someone please help me or indicate to me how to solve this problem?

Try to use the While loop

$query="SELECT image from tbl_images";

$results=mysql_query($query,$con);
while($row = mysql_fetch_array($results)){

echo $row['image'];
}

Read my thread here, i cover everything on this uploading and retreaving. I had the same issue.

http://www.daniweb.com/forums/thread246879.html

I found that the code can be maniplutated enought to call a picture in all proogramming languages.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.