Hi there,

I've started to create a site for me and my friends, and I want to be able to have each of us upload an image, and store it in a database, and then have it display as a display picture on a profile page.

I've been looking around, and nothing is solving my problem. Most answers display the image as an URL, but I want the actual image to display.

Cheers :)

When you upload the image, get the files location.

Store the file location into the database and when the profile page is viewed, echo a img tag with the location field as the 'src' value.

The other way would be to actually save the data from the image into a DB field, although I have never successfully done this so I cant help you if thats what you want to do.

Thanks heaps. Hopefully that solves my problem, sounds like it will.

You May Use The Below Code for Inserting Image into Database:

image.php File:

<?PHP
$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost 
$db_user = "username"; // change to your database password 
$db_password = "password"; // change to your database password 
$database = "database"; // provide your database name 
$db_table = "table"; // Your Table Name where you want to Store Your Image. 


# STOP HERE 
#################################################################### 
# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE 
$db = mysql_connect($hostname, $db_user, $db_password); 
mysql_select_db($database,$db);

$uploadDir = 'images/'; //Image Upload Folder

if(isset($_POST['Submit']))
{
$fileName = $_FILES['Photo']['name'];
$tmpName  = $_FILES['Photo']['tmp_name'];
$fileSize = $_FILES['Photo']['size'];
$fileType = $_FILES['Photo']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
	$filePath = addslashes($filePath);
}

$query = "INSERT INTO $db_table ( Image ) VALUES ('$filePath')";

mysql_query($query) or die('Error, query failed'); 

}
?>


image.htm File HTML Form Code:

<form name="Image" enctype="multipart/form-data" action="image.php" method="POST">
<input type="file" name="Photo" size="2000000" accept="image/gif, image/jpeg, image/x-ms-bmp, image/x-png" size="26"><br/>
<INPUT type="submit" class="button" name="Submit" value="  Submit  "> 
&nbsp;&nbsp;<INPUT type="reset" class="button" value="Cancel">
</form>


To View Your Image File in profile.php Page:

<?PHP
$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost 
$db_user = "username"; // change to your database password 
$db_password = "password"; // change to your database password 
$database = "database"; // provide your database name 
$db_table = "table"; // Your Table Name where you want to Store Your Image. 


# STOP HERE 
#################################################################### 
# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE 
$db = mysql_connect($hostname, $db_user, $db_password); 
mysql_select_db($database,$db);

$query = "SELECT * FROM $db_table WHERE username = '$username'";
$result = mysql_query($query) or die(mysql_error());
	while($row = mysql_fetch_array($result))
	{
echo "<img border=\"0\" src=\"".$row['Image']."\" width=\"102\" alt=\"Your Name\" height=\"91\">";
}
?>

Thanx...

You May Use The Below Code for Inserting Image into Database:

image.php File:

<?PHP
$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost 
$db_user = "username"; // change to your database password 
$db_password = "password"; // change to your database password 
$database = "database"; // provide your database name 
$db_table = "table"; // Your Table Name where you want to Store Your Image. 


# STOP HERE 
#################################################################### 
# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE 
$db = mysql_connect($hostname, $db_user, $db_password); 
mysql_select_db($database,$db);

$uploadDir = 'images/'; //Image Upload Folder

if(isset($_POST['Submit']))
{
$fileName = $_FILES['Photo']['name'];
$tmpName  = $_FILES['Photo']['tmp_name'];
$fileSize = $_FILES['Photo']['size'];
$fileType = $_FILES['Photo']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
	$filePath = addslashes($filePath);
}

$query = "INSERT INTO $db_table ( Image ) VALUES ('$filePath')";

mysql_query($query) or die('Error, query failed'); 

}
?>


image.htm File HTML Form Code:

<form name="Image" enctype="multipart/form-data" action="image.php" method="POST">
<input type="file" name="Photo" size="2000000" accept="image/gif, image/jpeg, image/x-ms-bmp, image/x-png" size="26"><br/>
<INPUT type="submit" class="button" name="Submit" value="  Submit  "> 
&nbsp;&nbsp;<INPUT type="reset" class="button" value="Cancel">
</form>


To View Your Image File in profile.php Page:

<?PHP
$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost 
$db_user = "username"; // change to your database password 
$db_password = "password"; // change to your database password 
$database = "database"; // provide your database name 
$db_table = "table"; // Your Table Name where you want to Store Your Image. 


# STOP HERE 
#################################################################### 
# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE 
$db = mysql_connect($hostname, $db_user, $db_password); 
mysql_select_db($database,$db);

$query = "SELECT * FROM $db_table WHERE username = '$username'";
$result = mysql_query($query) or die(mysql_error());
	while($row = mysql_fetch_array($result))
	{
echo "<img border=\"0\" src=\"".$row['Image']."\" width=\"102\" alt=\"Your Name\" height=\"91\">";
}
?>

Thanx...

You May Use The Below Code for Inserting Image into Database:

image.php File:

<?PHP
$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost 
$db_user = "username"; // change to your database password 
$db_password = "password"; // change to your database password 
$database = "database"; // provide your database name 
$db_table = "table"; // Your Table Name where you want to Store Your Image. 


# STOP HERE 
#################################################################### 
# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE 
$db = mysql_connect($hostname, $db_user, $db_password); 
mysql_select_db($database,$db);

$uploadDir = 'images/'; //Image Upload Folder

if(isset($_POST['Submit']))
{
$fileName = $_FILES['Photo']['name'];
$tmpName  = $_FILES['Photo']['tmp_name'];
$fileSize = $_FILES['Photo']['size'];
$fileType = $_FILES['Photo']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
	$filePath = addslashes($filePath);
}

$query = "INSERT INTO $db_table ( Image ) VALUES ('$filePath')";

mysql_query($query) or die('Error, query failed'); 

}
?>


image.htm File HTML Form Code:

<form name="Image" enctype="multipart/form-data" action="image.php" method="POST">
<input type="file" name="Photo" size="2000000" accept="image/gif, image/jpeg, image/x-ms-bmp, image/x-png" size="26"><br/>
<INPUT type="submit" class="button" name="Submit" value="  Submit  "> 
&nbsp;&nbsp;<INPUT type="reset" class="button" value="Cancel">
</form>


To View Your Image File in profile.php Page:

<?PHP
$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost 
$db_user = "username"; // change to your database password 
$db_password = "password"; // change to your database password 
$database = "database"; // provide your database name 
$db_table = "table"; // Your Table Name where you want to Store Your Image. 


# STOP HERE 
#################################################################### 
# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE 
$db = mysql_connect($hostname, $db_user, $db_password); 
mysql_select_db($database,$db);

$query = "SELECT * FROM $db_table WHERE username = '$username'";
$result = mysql_query($query) or die(mysql_error());
	while($row = mysql_fetch_array($result))
	{
echo "<img border=\"0\" src=\"".$row['Image']."\" width=\"102\" alt=\"Your Name\" height=\"91\">";
}
?>

Thanx...

i want to store pics name in database and photo in some folder....

<?php
mysql_connect("localhost", "db_username_here", "db_password_here") or die(mysql_error());
mysql_select_db("yourdbname") or die(mysql_error());
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$query = "SELECT ID FROM agents WHERE username = '$username'";
$result = mysql_query($query) or die('Error : ' . mysql_error());
$row = mysql_fetch_array($result);
$owner = $row['ID'];
$uploadDir = 'images/';

if(isset($_POST['submit']))
{
$fileName = $_FILES['file']['name'];
$tmpName  = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading <strong>file</strong>";
exit;
}

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
    $filePath = addslashes($filePath);
}

$result = mysql_query("UPDATE agents SET photo='images/$fileName' WHERE id='$owner'") 
or die(mysql_error());  

echo "$fileName <h1>Success!</h1> You have successfully added a Profile Photo. Please click <a href=index.php>here </a><br><br><br>";
}
else
{
echo" 
<form name=Image enctype=multipart/form-data method=POST>
<table cellspacing=2 cellpadding=2 border=0>
<tr><td>Image: </td><td><input type=file name=file accept=image/gif, image/jpeg, image/x-ms-bmp, image/x-png size=26 class=textbox /></td></tr>
 <tr><td> </td><td><input type=submit name=submit value=Upload class=button /></td></tr></table>
</form><br>";
}
?>
</div><div class="right_handler"><b>Search ID</b><br /><form action="tree.php" method="get"><input type="text" name="cue" size="25" /> <input type="submit" value="Submit" /></form></div>

Create a folder named "images" in the same directory where this file is. then to display:

<?php
// Connects to your Database
mysql_connect("localhost", "dbusername", "dbpassword") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());
$username = $_GET['username'];
$query = "SELECT photo FROM members WHERE username = '$username'";
$result = mysql_query($query) or die('Error : ' . mysql_error());
$row = mysql_fetch_array($result);
$photo = $row['photo'];


{
echo "<center><img src=images/$photo alt=Profile Photo with=200px>";
}
?>

Hope that helps

alos, if you want to assign a name/captio/tag for that, you have to create a field in the database and store it with the photo

This code to register ads from users....

<?php

include 'd:/xampp/htdocs/onlinedbc.php';

$action = $_GET['act'];

if ($action == 1){
$Category = $_POST['Category'];
$Activity = $_POST['Activity'];
$NameOfCompany = $_POST['NameOfCompany'];
$Address = $_POST['Address'];
$Phone = $_POST['Phone'];
$Fax = $_POST['Fax'];
$Mobile = $_POST['Mobile'];
$emailId = $_POST['emailId'];
$Website = $_POST['Website'];

$duplicate = mysql_query("select count(*) as total from users where Category='$Category' and Activity='$Activity' and NameOfCompany='$NameOfCompany' and Address='$Address' and Phone='$Phone' and Fax='$Fax' and Mobile='$Mobile' and emailId='$emailId' and Website='$Website'") or die(mysql_error());
list($total) = mysql_fetch_row($duplicate);

if ($total > 0)
{
$err = "***ERROR: The Company details already exists. Please try again with different category.***";
header("Location: kregister.php?msg=$err");
exit();
}

$sql_insert = "INSERT into `users`
(`Category`,`Activity`,`NameOfCompany`,`approved`,`Address`,`Phone`,`Fax`,`Mobile`,`emailId`,`Website`)
VALUES
('$Category','$Activity','$NameOfCompany','0','$Address','$Phone','$Fax','$Mobile','$emailId','$Website')
";


$a = mysql_query($sql_insert,$link) or die("Insertion Failed:" . mysql_error());
$row=mysql_query("select max(id) from users") or die("error");


if( $a > 0){
header("Location: image2.php");
}


}
?>

After insertin data i get id of a record   and send to image3.php by get method ...

image3.php:

 <?php


      $uploadDir = 'd:/images/';

       $id=$_GET['id'];

      if(isset($_POST['submit']))

      {

      $fileName = $_FILES['file']['name'];

      $tmpName = $_FILES['file']['tmp_name'];

      $fileSize = $_FILES['file']['size'];

      $fileType = $_FILES['file']['type'];



      $filePath = $uploadDir . $fileName;



      $result = move_uploaded_file($tmpName, $filePath);

      if (!$result) {

      echo "Error uploading <strong>file</strong>";

      exit;

      }



      if(!get_magic_quotes_gpc())

      {

      $fileName = addslashes($fileName);

      $filePath = addslashes($filePath);

      }

      $link=mysql_connect("67.220.196.234","hassaninfo","9845098450") or die("error1");

      mysql_select_db("hassaninfo", $link) or die("Couldn't select database");

      $result = mysql_query("UPDATE users SET photo='d:/$fileName' WHERE id='$id'")

      or die("error");



      echo "$fileName <h1>Success!</h1> You have successfully added a Profile Photo. Please click <a href=index.php>here </a><br><br><br>";

      }

      else

      {

      echo"

      <form name=Image enctype=multipart/form-data method=POST>

      <table cellspacing=2 cellpadding=2 border=0>

      <tr><td>Image: </td><td><input type=file name=file accept=image/gif, image/jpeg, image/x-ms-bmp, image/x-png size=26 class=textbox /></td></tr>

      <tr><td> </td><td><input type=submit name=submit value=Upload class=button /></td></tr></table>

      </form><br>";

      }

      ?>

But the prob is in update query it is showin error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '105''' at line 1

can u pls help me for this......photo name is not updating in database...if u any idea r code that insert image during registration pls send to me.........

just place $filename instead of d:/$fileName.
My first reply will do this for you. You just have to change Where "$owner" by your " $id ". After running a text, check the mysql database if it updated.

thank a lot.......it helps me to shape an idea ...

THANKS A LOT MEN! YOUR CODE HELPED ME A LOT! :)

I am getting an error "Error in uploading file". Please help me out of this.

how about inside database?the colume name?

Member Avatar for diafol

If this is a genuine query, start a new thread outlining your problem, giving details of your code.

je voudrais faire un système (galerie ) et afficher chaque photo du membre qui aura été enregistrer a la page d'acceuil du site

Member Avatar for diafol

Malheureusement, les règles stipulent que les questions doivent être posées en anglais seulement. Pardonnez mon français.

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.