Tuesday, March 15, 2016

Multiple Update image in PHP

Very easy and understandable code for multiple image uploading and updating.

<?php
$id=$_GET['id'];
session_start();
if(isset($_SESSION['user'])=="")
{
header("Location: home.php");
}
include 'dbconnect.php';

$sql="select * from dharampic where inv_id='".$_REQUEST['id']."'";
$r=mysql_query($sql) or die(mysql_error());
$q=mysql_fetch_array($r) or die(mysql_error());
$picid=$q['pic_id'];
$pic1=$q['prifile_pic'];
$pic2=$q['front'];
$pic3=$q['rside'];
$pic4=$q['lside'];
$pic5=$q['backside'];
$pic6=$q['dashboard'];
$pic7=$q['front_side'];
$pic8=$q['bask_side'];

?>

<form action='multipleupdate.php' method='post' enctype="multipart/form-data">
<input type="hidden" name="ids" value="<?php echo $picid;?>"/>
<tr>
<td align="right">Pic1</td>
<td>&nbsp;</td>
<td><input type="file" name="pic[]" />
<input type="hidden" name="p1" value="<?php echo $pic1;?>" />
&nbsp;</td>
</tr>
<tr>
<td align="right">&nbsp;</td>
<td>&nbsp;</td>
<td><img src="images/<?php echo $pic1;?>" width="50px" height="50px" />&nbsp;</td>
</tr>
<tr>
<td align="right">Pic2</td>
<td>&nbsp;</td>
<td><input type="file" name="pic[]" />
<input type="hidden" name="p2" value="<?php echo $pic2;?>" />
&nbsp;</td>
</tr>
<tr>
<td align="right">&nbsp;</td>
<td>&nbsp;</td>
<td><img src="images/<?php echo $pic2;?>" alt="" width="50px" height="50px" /></td>
</tr>
<tr>
<td align="right">Pic3</td>
<td>&nbsp;</td>
<td><input type="file" name="pic[]" />
<input type="hidden" name="p3" value="<?php echo $pic3;?>" />
&nbsp;</td>
</tr>
<tr>
<td align="right">&nbsp;</td>
<td>&nbsp;</td>
<td><img src="images/<?php echo $pic3;?>" alt="" width="50px" height="50px" /></td>
</tr>
<tr>
<td align="right">Pic4</td>
<td>&nbsp;</td>
<td><input type="file" name="pic[]" />
<input type="hidden" name="p4" value="<?php echo $pic4;?>" />
&nbsp;</td>
</tr>
<tr>
<td align="right">&nbsp;</td>
<td>&nbsp;</td>
<td><img src="images/<?php echo $pic4;?>" alt="" width="50px" height="50px" /></td>
</tr>
<tr>
<td align="right">Pic5</td>
<td>&nbsp;</td>
<td><input type="file" name="pic[]" />
<input type="hidden" name="p5" value="<?php echo $pic5;?>" />
&nbsp;</td>
</tr>
<tr>
<td align="right">&nbsp;</td>
<td>&nbsp;</td>
<td><img src="images/<?php echo $pic5;?>" alt="" width="50px" height="50px" /></td>
</tr>
<tr>
<td align="right">Pic6</td>
<td>&nbsp;</td>
<td><input type="file" name="pic[]" />
<input type="hidden" name="p6" value="<?php echo $pic6;?>" />
&nbsp;</td>
</tr>
<tr>
<td align="right">&nbsp;</td>
<td>&nbsp;</td>
<td><img src="images/<?php echo $pic6;?>" alt="" width="50px" height="50px" /></td>
</tr>
<tr>
<td align="right">Pic7</td>
<td>&nbsp;</td>
<td><input type="file" name="pic[]" />
<input type="hidden" name="p7" value="<?php echo $pic7;?>" />
&nbsp;</td>
</tr>
<tr>
<td align="right">&nbsp;</td>
<td>&nbsp;</td>
<td><img src="images/<?php echo $pic7;?>" alt="" width="50px" height="50px" /></td>
</tr>
<tr>
<td align="right">Pic8</td>
<td>&nbsp;</td>
<td><input type="file" name="pic[]" />
<input type="hidden" name="p8" value="<?php echo $pic8;?>" />
&nbsp;</td>
</tr>
<tr>
<td align="right">&nbsp;</td>
<td>&nbsp;</td>
<td><img src="images/<?php echo $pic8;?>" alt="" width="50px" height="50px" /></td>
</tr>
<tr><td><input type="submit" name="submit" value="Submit" /></td></tr>
</form>







multipleupdate.php


<?php
if(isset($_POST['submit'])){
$picid=$_POST['ids'];
$path='';
$val=array();
if(count($_FILES["pic"]['name'])>0)
 { 
 //echo count($_FILES["pic"]['name']);
//check if any file uploaded
 //$GLOBALS['msg'] = ""; //initiate the global message
 $i=0;
 foreach($_FILES["pic"]['name'] as $data=>$key){
 $i++;
 if(move_uploaded_file($_FILES["pic"]["tmp_name"][$i],'images/'.$key)){
 //$filen = $_FILES["pic"]['name'][$i]; //file name
 if($key==''){
 $valimg=$_POST['p'.$i];
 $path = 'p'.$i.'="'.$valimg.'",'; //generate the destination path
 }
 else{
 $path = ' p'.$i.'="images/'.$key.'",'; //generate the destination path
 }
 array_push($val,$path);
}
}
$str=implode(" ",$val);
$str=rtrim($str,',');
 echo "update dharampic set ".$str." where pic_id=".$picid;
}
}
?>

No comments:

Post a Comment

Dharamart.blogspot.in