ghianita Posted May 19, 2014 Share Posted May 19, 2014 One image is displaying but when i choose image 2 it overlaps image 1..gallery.php Quote Link to comment https://forums.phpfreaks.com/topic/288596-how-to-upload-two-images-in-a-same-row-and-save-it-to-database/ Share on other sites More sharing options...
IanA Posted May 19, 2014 Share Posted May 19, 2014 Try giving the file inputs unique names as they currently are using the same name. Quote Link to comment https://forums.phpfreaks.com/topic/288596-how-to-upload-two-images-in-a-same-row-and-save-it-to-database/#findComment-1480030 Share on other sites More sharing options...
ghianita Posted May 19, 2014 Author Share Posted May 19, 2014 i gave different folder for two image box. which started as gfilename1 and second picture will save as g2filename2.. Quote Link to comment https://forums.phpfreaks.com/topic/288596-how-to-upload-two-images-in-a-same-row-and-save-it-to-database/#findComment-1480048 Share on other sites More sharing options...
ghianita Posted May 20, 2014 Author Share Posted May 20, 2014 How to upload two images in same row of table I got problem on while browsing file when i browse first image and insert imagename it works but when i browse second (picture 2) it overlaps first image but Image name will shown correctly.. Please help me out..  Here is the Code:  <?php require_once("../database/connection.php"); $conn = new dbconnect(); $c = $conn->connect(); ?>  <style type="text/css"> <!-- .style1 { color: #FFFFFF; font-weight: bold; } .style2 {color: #FFFFFF}  .style4 { color: #FFFFFF; font-size: 18px; font-weight: bold; } a{ color:#FFFFFF; } --> </style>  <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Puspa Lal Foundation Admin</title> <link rel="shortcut icon" href="../favicon/081809_puspa lal.jpg"/> </head> <link class="jsbin" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" /> <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script> <meta charset=utf-8 />  <script language="javascript"> function readURL(input) { if (input.files && input.files[0]) { var reader = new FileReader();  reader.onload = function (e) { $('#blah') .attr('src', e.target.result) .width(150) .height(200); }; reader.readAsDataURL(input.files[0]); } } </script>  <?php $viewTag = "N";  if(isset($_REQUEST['eimageName1'])) { $id = $_REQUEST['eimageName1']; $query = "SELECT gId, imageName1, imageName2, fileName1, fileName2, postedOn FROM gallery  where gId = '$id'"; $rec = $conn->selectData($c, $query);  if(isset($rec)) { foreach($rec as $row) { $gId = $row['gId']; $imageName1 = $row['imageName1']; $imageName2 = $row['imageName2']; $fileName1 = $row['fileName1']; $fileName2 = $row['fileName2']; $postedOn = $row['postedOn']; } } }   if(isset($_POST['save'])) { $imageName1 = $_POST['imageName1']; $imageName2 = $_POST['imageName2']; $postedOn = $_POST['postedOn']; $fileName1 = "g".$_FILES["file"]["name"]; move_uploaded_file($_FILES["file"]["tmp_name"],    "../admin/Images/PhotoGallery/g" . $_FILES["file"]["name"]); $fileName2 = "g2".$_FILES["file"]["name"]; move_uploaded_file($_FILES["file"]["tmp_name"],    "../admin/Images/g2" . $_FILES["file"]["name"]);  if($gId == null) { $query = "insert into gallery (imageName1, imageName2, fileName1, fileName2,  postedOn) values ('$imageName1', '$imageName2', '$fileName1', '$fileName2',  curdate())"; $conn->InEdDeData($c, $query); } else { $query = "update gallery set imageName1 = '$imageName1', imageName2 = '$imageName2', fileName1 = '$fileName1' , fileName2 = '$fileName2', postedOn =  sysdate where gId = '$gId'"; $conn->InEdDeData($c, $query); } }  else if(isset($_POST['delete'])) { $query = "delete from gallery where gId = '$gId'"; $conn->InEdDeData($c, $query);  $gId = null; $imageName1 = null; $imageName2 = null; $fileName1 = null; $fileName2 = null; $postedOn = null;  $id = null; $_REQUEST['eimageName1'] = null; } else if(isset($_POST['clear'])) { $gId = null; $imageName1 = null; $imageName2 = null; $fileName1 = null; $fileName2 = null; $postedOn = null;  $id = null; $_REQUEST['eimageName1'] = null; }  if(isset($_POST['view'])) $viewTag = "Y"; else if(isset($_POST['hide'])) $viewTag = "N";  ?>   <form name="form1" action="" method="post" enctype="multipart/form-data"> <table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#666666"> <tr> <td align="left" valign="top" bgcolor="#CCCCCC" class="nav-admin" style="padding-left:10px;">   <div align="right"><span class="style2"><a href="main_index.php"><img src="adminpics/icons/home button.png" width="50" height="50" /></a>  <a href="index.php?logout=1"><img src="adminpics/icons/Perspective-Button-Logoff-icon.png" width="50" height="50"/> </a>     </span></div> </td> </tr> </table>  <table width="100%" border="0" cellspacing="0" cellpadding="10" align="center"> <tr> <td colspan="2" bgcolor="#000000" align="center">   <span class="style4">Photo Gallery</span></td></tr> <tr bgcolor="#98AFC7"> <td width="60%"> <table width="100%"> <tr>   <td bgcolor="#98AFC7"><div align="right" class="style2"><strong>Picture 1</strong></div></td>    <td bgcolor="#98AFC7"><div align="center" class="style2"><strong>:</strong></div></td>     <td>    <?php if($fileName1 == "") echo '<label><img id="blah" src="#" alt="your image" height="80%" width="100%"/></label>'; else echo '<label><img id="blah" src="../admin/Images/PhotoGallery/'.$fileName1.'" alt="your image" height="80%" width="100%"/></label>'; ?> <input type="file"  onchange="readURL(this);" id="file" name="file" /> </tr>    <tr>   <td bgcolor="#98AFC7"><div align="right" class="style2"><strong>Image Name 1</strong></div> </td> <td bgcolor="#98AFC7"><div align="center" class="style2"><strong>:</strong></div></td>    <td bgcolor="#98AFC7"><span class="style2">      <label>     <input name="imageName1" type="text" id="imageName1" />     </label></span></td> </tr>    <tr>   <td bgcolor="#98AFC7"><div align="right" class="style2"><strong>Picture 2</strong></div></td>    <td bgcolor="#98AFC7"><div align="center" class="style2"><strong>:</strong></div></td>     <td>    <?php if($fileName2 == "") echo '<label><img id="blah" src="#" alt="your image" height="80%" width="100%"/></label>'; else echo '<label><img id="blah" src="../admin/Images/'.$fileName2.'" alt="your image" height="80%" width="100%"/></label>'; ?>  <input type="file"  onchange="readURL(this);" id="file" name="file" /> </tr>  <tr>   <td bgcolor="#98AFC7"><div align="right" class="style2"><strong>Image Name 2 </strong></div> </td> <td bgcolor="#98AFC7"><div align="center" class="style2"><strong>:</strong></div></td> <td bgcolor="#98AFC7"><span class="style2">      <label>     <input name="imageName2" type="text" id="imageName2" />     </label></span></td> </tr>   <tr> <td bgcolor="#98AFC7" height="47" valign="top"> </td>   <td bgcolor="#98AFC7" height="47" valign="top"> </td>     <td bgcolor="#98AFC7">              <input name="save" type="submit" id="save" value="Save" />       <input name="delete" type="submit" id="delete" value="Delete" /> <?php     if($viewTag == "N")       echo '<input name="view" type="submit" id="view" value="View All"/>';     else       echo '<input name="hide" type="submit" id="hide" value="Hide All"/>'; ?>      <a href="gallery.php"><input name="clear" type="button" id="clear" value="Clear" /></a>     </td></tr> </table>  </table>      </form>  <?php /* start of the list part */ if($viewTag == "Y") { $query = "SELECT gId, imageName1, imageName2, fileName1, fileName2, postedOn FROM gallery order by fileName1"; $rec = $conn->selectData($c, $query);  $rw = 0; if(isset($rec)) { foreach($rec as $row) { $g[$rw][0] = $row['gId']; $g[$rw][1] = $row['imageName1']; $g[$rw][2] = $row['imageName2']; $g[$rw][3] = $row['fileName1']; $g[$rw][4] = $row['fileName2']; $g[$rw][5] = $row['postedOn'];  $rw++; } }  ?> <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"> <tr><td colspan="2" bgcolor="#000000">     <div align="center" class="style2"><strong>Photo Gallery</strong></div> </td></tr> <tr><td> <table width="100%" border="0">     <tr bgcolor="#98AFC7">     <th>SN</th>     <th>Image</th>     <th>Image Name</th>     <th>Image</th>     <th>Image Name</th>     <th>Posted On</th>     <th>Action</th>     <tr>  <?php $cnt = count($g); if($cnt > 0) { for($a = 0; $a < $cnt; $a++) { ?> <tr bgcolor="#98AFC7"> <td align="center"><?php echo ($a+1); ?></td> <?php           echo '<td><img id="blah" src="../admin/Images/PhotoGallery/'.$g[$a][3].'" alt="your image" height="25%" width="30%"/>'; ?> <td align="center"><?php echo $g[$a][1]; ?></td> <?php           echo '<td><img id="blah" src="../admin/Images/'.$g[$a][4].'" alt="your image" height="25%" width="30%"/>'; ?> <td align="center"><?php echo $g[$a][2]; ?></td>         <td align="center"><?php echo $g[$a][5]; ?></td> <td align="center" valign="middle">         <a href="gallery.php?eimageName1=<?php echo $g[$a][0]; ?>">[Edit]</a></td>         </tr> <?php } }  ?>  <?php }  ?> </table>  </td></tr> </table>  Quote Link to comment https://forums.phpfreaks.com/topic/288596-how-to-upload-two-images-in-a-same-row-and-save-it-to-database/#findComment-1480148 Share on other sites More sharing options...
IanA Posted May 20, 2014 Share Posted May 20, 2014 (edited) What I am referring to are your inputs: Â <input type="file" Â onchange="readURL(this);" id="file" name="file" /> Â You have two of these with the same names and therefore they can't be distinguished between the two and this is why, I believe, the second file is overriding the file in the first input. Â You will, of course, need to change some logic in your PHP code so that it will retrieve the correct variables following the change in names. Edited May 20, 2014 by IanA Quote Link to comment https://forums.phpfreaks.com/topic/288596-how-to-upload-two-images-in-a-same-row-and-save-it-to-database/#findComment-1480152 Share on other sites More sharing options...
IanA Posted May 20, 2014 Share Posted May 20, 2014 (edited) Have you simply re-posted the original issue? Have you tried my suggestion? Edited May 20, 2014 by IanA Quote Link to comment https://forums.phpfreaks.com/topic/288596-how-to-upload-two-images-in-a-same-row-and-save-it-to-database/#findComment-1480158 Share on other sites More sharing options...
ghianita Posted May 21, 2014 Author Share Posted May 21, 2014 ya bt it didn't work so can u please teach me how to work on this... Quote Link to comment https://forums.phpfreaks.com/topic/288596-how-to-upload-two-images-in-a-same-row-and-save-it-to-database/#findComment-1480252 Share on other sites More sharing options...
Barand Posted May 21, 2014 Share Posted May 21, 2014 http://www.php.net/manual/en/features.file-upload.multiple.php Quote Link to comment https://forums.phpfreaks.com/topic/288596-how-to-upload-two-images-in-a-same-row-and-save-it-to-database/#findComment-1480269 Share on other sites More sharing options...
ghianita Posted May 21, 2014 Author Share Posted May 21, 2014 Still not worked... Quote Link to comment https://forums.phpfreaks.com/topic/288596-how-to-upload-two-images-in-a-same-row-and-save-it-to-database/#findComment-1480280 Share on other sites More sharing options...
IanA Posted May 21, 2014 Share Posted May 21, 2014 It would appear that the main reason the image is being 'overwritten' is because either: Â 1) You haven't properly implemented the examples seen in Barand's link 2) If the file input is now correct, that your PHP is not referencing the uploaded file array correctly. Â Please attach the php file following your changes so we can see whether they've been implemented correctly. Quote Link to comment https://forums.phpfreaks.com/topic/288596-how-to-upload-two-images-in-a-same-row-and-save-it-to-database/#findComment-1480281 Share on other sites More sharing options...
ghianita Posted May 21, 2014 Author Share Posted May 21, 2014 <?php require_once("../database/connection.php"); $conn = new dbconnect(); $c = $conn->connect(); ?>  <style type="text/css"> <!-- .style1 { color: #FFFFFF; font-weight: bold; } .style2 {color: #FFFFFF}  .style4 { color: #FFFFFF; font-size: 18px; font-weight: bold; } a{ color:#FFFFFF; } --> </style>  <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Puspa Lal Foundation Admin</title> <link rel="shortcut icon" href="../favicon/081809_puspa lal.jpg"/> </head> <link class="jsbin" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" /> <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script> <meta charset=utf-8 />  <script language="javascript"> function readURL(input) { if (input.files && input.files[0]) { var reader = new FileReader();  reader.onload = function (e) { $('#blah') .attr('src', e.target.result) .width(150) .height(200); }; reader.readAsDataURL(input.files[0]); } } </script>  <?php $viewTag = "N";  if(isset($_REQUEST['eimageName1'])) { $id = $_REQUEST['eimageName1']; $query = "SELECT gId, imageName1, imageName2, fileName1, fileName2, postedOn FROM gallery  where gId = '$id'"; $rec = $conn->selectData($c, $query);  if(isset($rec)) { foreach($rec as $row) { $gId = $row['gId']; $imageName1 = $row['imageName1']; $imageName2 = $row['imageName2']; $fileName1 = $row['fileName1']; $fileName2 = $row['fileName2']; $postedOn = $row['postedOn']; } } }   if(isset($_POST['save'])) { $imageName1 = $_POST['imageName1']; $imageName2 = $_POST['imageName2']; $postedOn = $_POST['postedOn']; $fileName1 = "g".$_FILES["file"]["name"]; move_uploaded_file($_FILES["file"]["tmp_name"],    "../admin/Images/PhotoGallery/g" . $_FILES["file"]["name"]); $fileName2 = "g2".$_FILES["file"]["name"]; move_uploaded_file($_FILES["file"]["tmp_name"],    "../admin/Images/PhotoGallery/g2" . $_FILES["file"]["name"]);  if($gId == null) { $query = "insert into gallery (imageName1, imageName2, fileName1, fileName2,  postedOn) values ('$imageName1', '$imageName2', '$fileName1', '$fileName2',  curdate())"; $conn->InEdDeData($c, $query); } else { $query = "update gallery set imageName1 = '$imageName1', imageName2 = '$imageName2', fileName1 = '$fileName1' , fileName2 = '$fileName2', postedOn =  sysdate where gId = '$gId'"; $conn->InEdDeData($c, $query); } }  else if(isset($_POST['delete'])) { $query = "delete from gallery where gId = '$gId'"; $conn->InEdDeData($c, $query);  $gId = null; $imageName1 = null; $imageName2 = null; $fileName1 = null; $fileName2 = null; $postedOn = null;  $id = null; $_REQUEST['eimageName1'] = null; } else if(isset($_POST['clear'])) { $gId = null; $imageName1 = null; $imageName2 = null; $fileName1 = null; $fileName2 = null; $postedOn = null;  $id = null; $_REQUEST['eimageName1'] = null; }  if(isset($_POST['view'])) $viewTag = "Y"; else if(isset($_POST['hide'])) $viewTag = "N";  ?>   <form name="form1" action="" method="post" enctype="multipart/form-data"> <table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#666666"> <tr> <td align="left" valign="top" bgcolor="#CCCCCC" class="nav-admin" style="padding-left:10px;">   <div align="right"><span class="style2"><a href="main_index.php"><img src="adminpics/icons/home button.png" width="50" height="50" /></a>  <a href="index.php?logout=1"><img src="adminpics/icons/Perspective-Button-Logoff-icon.png" width="50" height="50"/> </a>     </span></div> </td> </tr> </table>  <table width="100%" border="0" cellspacing="0" cellpadding="10" align="center"> <tr> <td colspan="2" bgcolor="#000000" align="center">   <span class="style4">Photo Gallery</span></td></tr> <tr bgcolor="#98AFC7"> <td width="60%"> <table width="100%"> <tr>   <td bgcolor="#98AFC7"><div align="right" class="style2"><strong>Picture 1</strong></div></td>    <td bgcolor="#98AFC7"><div align="center" class="style2"><strong>:</strong></div></td>     <td>    <?php if($fileName1 == "") echo '<label><img id="blah" src="#" alt="your image" height="80%" width="100%"/></label>'; else echo '<label><img id="blah" src="../admin/Images/PhotoGallery/'.$fileName1.'" alt="your image" height="80%" width="100%"/></label>'; ?> <input type="file"  onchange="readURL(this);" id="file" name="file" /> </tr>    <tr>   <td bgcolor="#98AFC7"><div align="right" class="style2"><strong>Image Name 1</strong></div> </td> <td bgcolor="#98AFC7"><div align="center" class="style2"><strong>:</strong></div></td>    <td bgcolor="#98AFC7"><span class="style2">      <label>     <input name="imageName1" type="text" id="imageName1" />     </label></span></td> </tr>    <tr>   <td bgcolor="#98AFC7"><div align="right" class="style2"><strong>Picture 2</strong></div></td>    <td bgcolor="#98AFC7"><div align="center" class="style2"><strong>:</strong></div></td>     <td>    <?php if($fileName2 == "") echo '<label><img id="blah" src="#" alt="your image" height="80%" width="100%"/></label>'; else echo '<label><img id="blah" src="../admin/PhotoGallery/'.$fileName2.'" alt="your image" height="80%" width="100%"/></label>'; ?>  <input type="file"  onchange="readURL(this);" id="file2" name="file2" /> </tr>  <tr>   <td bgcolor="#98AFC7"><div align="right" class="style2"><strong>Image Name 2 </strong></div> </td> <td bgcolor="#98AFC7"><div align="center" class="style2"><strong>:</strong></div></td> <td bgcolor="#98AFC7"><span class="style2">      <label>     <input name="imageName2" type="text" id="imageName2" />     </label></span></td> </tr>   <tr> <td bgcolor="#98AFC7" height="47" valign="top"> </td>   <td bgcolor="#98AFC7" height="47" valign="top"> </td>     <td bgcolor="#98AFC7">              <input name="save" type="submit" id="save" value="Save" />       <input name="delete" type="submit" id="delete" value="Delete" /> <?php     if($viewTag == "N")       echo '<input name="view" type="submit" id="view" value="View All"/>';     else       echo '<input name="hide" type="submit" id="hide" value="Hide All"/>'; ?>      <a href="gallery.php"><input name="clear" type="button" id="clear" value="Clear" /></a>     </td></tr> </table>  </table>      </form>  <?php /* start of the list part */ if($viewTag == "Y") { $query = "SELECT gId, imageName1, imageName2, fileName1, fileName2, postedOn FROM gallery order by fileName1"; $rec = $conn->selectData($c, $query);  $rw = 0; if(isset($rec)) { foreach($rec as $row) { $g[$rw][0] = $row['gId']; $g[$rw][1] = $row['imageName1']; $g[$rw][2] = $row['imageName2']; $g[$rw][3] = $row['fileName1']; $g[$rw][4] = $row['fileName2']; $g[$rw][5] = $row['postedOn'];  $rw++; } }  ?> <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"> <tr><td colspan="2" bgcolor="#000000">     <div align="center" class="style2"><strong>Photo Gallery</strong></div> </td></tr> <tr><td> <table width="100%" border="0">     <tr bgcolor="#98AFC7">     <th>SN</th>     <th>Image</th>     <th>Image Name</th>     <th>Image</th>     <th>Image Name</th>     <th>Posted On</th>     <th>Action</th>     <tr>  <?php $cnt = count($g); if($cnt > 0) { for($a = 0; $a < $cnt; $a++) { ?> <tr bgcolor="#98AFC7"> <td align="center"><?php echo ($a+1); ?></td> <?php           echo '<td><img id="blah" src="../admin/Images/PhotoGallery/'.$g[$a][3].'" alt="your image" height="25%" width="30%"/>'; ?> <td align="center"><?php echo $g[$a][1]; ?></td> <?php           echo '<td><img id="blah" src="../admin/Images/'.$g[$a][4].'" alt="your image" height="25%" width="30%"/>'; ?> <td align="center"><?php echo $g[$a][2]; ?></td>         <td align="center"><?php echo $g[$a][5]; ?></td> <td align="center" valign="middle">         <a href="gallery.php?eimageName1=<?php echo $g[$a][0]; ?>">[Edit]</a></td>         </tr> <?php } }  ?>  <?php }  ?> </table>   </td></tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/288596-how-to-upload-two-images-in-a-same-row-and-save-it-to-database/#findComment-1480283 Share on other sites More sharing options...
IanA Posted May 21, 2014 Share Posted May 21, 2014 (edited) Please make use of the code tag (seen in the toolbar) as this is quite difficult to read. You have highlighted some code in red...are you telling us that this is what you've changed, because if so, it would appear you haven't made changes to to reflect the link Barand gave you.  Change your file inputs to the following:  <input type="file"  onchange="readURL(this);" id="file" name="file[]" /> <input type="file"  onchange="readURL(this);" id="file2" name="file[]" /> Edited May 21, 2014 by IanA Quote Link to comment https://forums.phpfreaks.com/topic/288596-how-to-upload-two-images-in-a-same-row-and-save-it-to-database/#findComment-1480285 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.