0

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.

Edited by DARK_BYTE: n/a

4
Contributors
5
Replies
7
Views
7 Years
Discussion Span
Last Post by zinnqu
0

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?

Edited by DARK_BYTE: n/a

0

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'];
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.