How To upload, preview and save images in data base?

How To upload, preview and save images in data base?

hi Friends,

I am using a form in which i am uploading 4 images but when i getting the preview it only show first image every 4 time. 
pls look into code and suggest me.

here is the code registration page.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome to Make My Jain Shaadi</title>
<link rel="stylesheet" type="text/css" href="css/main-style.css">
<link rel="stylesheet" type="text/css" href="css/nivo-slider.css">

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function() 
//$('#showall').click( function () {
$('#candidate-photo').live('change', function()
$("#preview").html('');
$("#preview").html('<img src="loader.gif" alt="Uploading...."/>');
$("#registration").ajaxForm(
{
target: '#preview'
}).submit();
});

$('#candidate-father').live('change', function()
$("#preview1").html('');
$("#preview1").html('<img src="loader.gif" alt="Uploading...."/>');
$("#registration").ajaxForm(
{
target: '#preview1'
}).submit();
});

$('#candidate-family').live('change', function()
$("#preview2").html('');
$("#preview2").html('<img src="loader.gif" alt="Uploading...."/>');
$("#registration").ajaxForm(
{
target: '#preview2'
}).submit();
});

$('#candidate-mother').live('change', function()
$("#preview3").html('');
$("#preview3").html('<img src="loader.gif" alt="Uploading...."/>');
$("#registration").ajaxForm(
{
target: '#preview3'
}).submit();
});

}); 
</script>
<style>

body
{
font-family:arial;
}
.preview
{
width:100px;
height:120px;
border:solid 1px #dedede;
padding:10px;
}
#preview
{
color:#cc0000;
font-size:12px
}
.preview1
{
width:100px;
height:120px;
border:solid 1px #dedede;
padding:10px;
}
#preview1
{
color:#cc0000;
font-size:12px
}
.preview2
{
width:100px;
height:120px;
border:solid 1px #dedede;
padding:10px;
}
#preview2
{
color:#cc0000;
font-size:12px
}
.preview3
{
width:100px;
height:120px;
border:solid 1px #dedede;
padding:10px;
}
#preview3
{
color:#cc0000;
font-size:12px
}
</style>

</head>

<body>

<form id="registration" name="registration" action="ajaximagemain.php" method="post">
<table width="940" bgcolor="#f1f1f1"  cellspacing="0" cellpadding="8" align="left" style="background:url(mmjs-image/form-bg1.jpg) no-repeat; margin-left:10px;" >

<tr bgcolor="#FFFFFF" style="text-align:left">
<td>Upload Photo of Candidate:</td>
<td><input name="candidate-photo" id="candidate-photo" size="15"  type="file" style="width:50px;"/>
<input align="right" name="upload_candidate" id="upload_candidate" type="button" value="Upload" style=" float: right; margin-right:140px;"/></td>
</tr>

<tr bgcolor="#FFFFFF" style="text-align:left">
<td>Upload Photo of Father:</td>
<td><input name="candidate-father" id="candidate-father"  size="15" type="file" style="width:90px;"/>
<input align="right" name="upload_father" id="upload_father" type="button" value="Upload" style=" float: right; margin-right:140px;"/></td>
</tr>

<tr bgcolor="#FFFFFF" style="text-align:left">
<td>Upload Family Group photo</td>
<td><input name="candidate-family" id="candidate-family"  size="15" type="file" style="width:90px;"/>
<input align="right" name="upload_family" id="upload_family" type="button" value="Upload" style=" float: right; margin-right:140px;"/></td>
</tr>

<tr bgcolor="#FFFFFF" style="text-align:left">
<td>Upload Photo of Monther:</td>
<td><input name="candidate-mother" id="candidate-mother" size="15"  type="file" style="width:90px;"/>
<input align="right" name="upload_mother" id="upload_mother" type="button" value="Upload" style=" float: right; margin-right:140px;"/></td>
</tr>

</table>

</form>
</body>
</html>

form ajaximagemain.php code

<?php
include('db.php');
session_start();
$session_id='1'; //$session id
$path = "uploads/";

$valid_formats = array("jpg", "png", "gif", "bmp");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['candidate-photo']['name'];
$size = $_FILES['candidate-photo']['size'];
$name1 = $_FILES['candidate-father']['name'];
$size1 = $_FILES['candidate-father']['size'];
$name2 = $_FILES['candidate-family']['name'];
$size2 = $_FILES['candidate-family']['size'];
$name3 = $_FILES['candidate-mother']['name'];
$size3 = $_FILES['candidate-mother']['size'];
if(strlen($name))
{
list($txt,$ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024))
{
$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
$tmp = $_FILES['candidate-photo']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
//mysql_query("UPDATE users_image SET profile_image='$actual_image_name' WHERE uid='$session_id'");
// mysql_query("insert into users_image (username,password,email,profile_image,profile_image_small) values('$name','$name','$ name@gmail.com','$actual_image_name','$actual_image_name')",$bd)or die("Error in update!:" . mysql_error());
echo "<img src='uploads/".$actual_image_name."'  class='preview'>";
}
else
echo "failed";
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format..";
}
/////////////////22222
else if(strlen($name1))
{
list($txt1, $ext1) = explode(".", $name1);
if(in_array($ext1,$valid_formats))
{
if($size1<(1024*1024))
{
$actual_image_name1 = time().substr(str_replace(" ", "_", $txt1), 5).".".$ext1;
$tmp1 = $_FILES['candidate-father']['tmp_name'];
if(move_uploaded_file($tmp1, $path.$actual_image_name1))
{
//mysql_query("UPDATE users_image SET profile_image='$actual_image_name' WHERE uid='$session_id'");
//@mysql_query("insert into users_image (username,password,email,profile_image,profile_image_small) values('$name','$name','$ name@gmail.com','$actual_image_name','$actual_image_name')",$bd)or die("Error in update!:" . mysql_error());
echo "<img src='uploads/".$actual_image_name1."' class='preview1'>";
}
else
echo "failed";
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format..";
}
//222222222222
//333333333333

 else if(strlen($name2))
{
list($txt2, $ext2) = explode(".", $name2);
if(in_array($ext2,$valid_formats))
{
if($size2<(1024*1024))
{
$actual_image_name2 = time().substr(str_replace(" ", "_", $txt2), 5).".".$ext2;
$tmp2 = $_FILES['candidate-family']['tmp_name'];
if(move_uploaded_file($tmp2, $path.$actual_image_name2))
{
//mysql_query("UPDATE users_image SET profile_image='$actual_image_name' WHERE uid='$session_id'");
// @mysql_query("insert into users_image (username,password,email,profile_image,profile_image_small) values('$name','$name','$ name@gmail.com','$actual_image_name','$actual_image_name')",$bd)or die("Error in update!:" . mysql_error());
echo "<img src='uploads/".$actual_image_name2."'  class='preview2'>";
}
else
echo "failed";
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format..";
}
///////33333333333333
////444444
else if(strlen($name3))
{
list($txt3, $ext3) = explode(".", $name3);
if(in_array($ext3,$valid_formats))
{
if($size3<(1024*1024))
{
$actual_image_name3 = time().substr(str_replace(" ", "_", $txt3), 5).".".$ext3;
$tmp3 = $_FILES['candidate-mother']['tmp_name'];
if(move_uploaded_file($tmp3, $path.$actual_image_name3))
{
//mysql_query("UPDATE users_image SET profile_image='$actual_image_name' WHERE uid='$session_id'");
// @mysql_query("insert into users_image (username,password,email,profile_image,profile_image_small) values('$name','$name','$ name@gmail.com','$actual_image_name','$actual_image_name')",$bd)or die("Error in update!:" . mysql_error());
echo "<img src='uploads/".$actual_image_name3."'  class='preview3'>";
}
else
echo "failed";
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format..";
}
////444444
else
echo "Please select image..!";
// exit;
}
?>