Jump to content

How to upload two images in a same row and save it to database


ghianita

Recommended Posts

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" />
<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>
 
Link to comment
Share on other sites

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 by IanA
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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" />



<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>
Link to comment
Share on other sites

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 by IanA
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.