Jump to content

last row overwriting the previous row in the table.


erok

Recommended Posts

Hi all ,

 

I am trying to write some codes which needs to be able to upload multiple images into separate rows in a table  within one form.

 

i only got the last picture inserted twice to mysqli database table, instead of inserting the first picture in the form first and moving to the second picture next.

Any direction or help greatly appreciated.

 

My code like this ;

 

if (!empty($_POST['kitchen']))
   {                                                                       
//Assign Variables
$path = $_FILES['image']['tmp_name'] ;
$name = $_FILES['image']['name'] ;
$type = $_FILES['image']['type'] ;
$error= $_FILES['image']['error'] ;
$caption = $_POST['caption'] ;            
 
// read file contents
$content2 = file_get_contents($path);
$content = mysqli_real_escape_string($con, $content2);
$caption = mysqli_real_escape_string($con, $caption) ;
$bol = "insert into dakimages (name, type, content, caption) values ('{$name}', '{$type}', '{$content}', '{$caption}');";
$soy = mysqli_query($con, $bol) ;
 }
if (!empty($_POST['kitchen']))
   {                                                                      
//Assign Variables
$path = $_FILES['image']['tmp_name'] ;
$name = $_FILES['image']['name'] ;
$type = $_FILES['image']['type'] ;
$error= $_FILES['image']['error'] ;
$caption = $_POST['caption'] ;
          
// read file contents
$content2 = file_get_contents($path);
$content = mysqli_real_escape_string($con, $content2);
$caption = mysqli_real_escape_string($con, $caption) ;
                
 // insert into table
      $dor = "insert into dakimages (name, type, content, caption) values ('{$name}', '{$type}', '{$content}', '{$caption}');";
      $scl = mysqli_query($con, $dor) ;
    }
?>
And this is form ;
 
<input type="file" name="image" />
<br />
<br />
<span class="margin1">Caption for photo (1)</span>
<br />
<textarea type="text" name="caption" maxlength="150"  rows="3" col="85"></textarea>
<br />
<input type="file" name="image" />
<br />
<span class="margin1">Caption for photo (2)</span>
<br />
<textarea type="text" name="caption" maxlength="150"  rows="3" col="85"></textarea>
<br />
<input type="submit" name="kitchen" id="forminsert" value="upload all" size="35" />

and then process the array something like this

$img_count = count($_FILES['image']['name']);
for($i=0;$i<$img_count;$i++) {
   $file_name = $_FILES['image']['name'][$i];
   $file_type = $_FILES['image']['type'][$i];
   $file_size = $_FILES['image']['size'][$i];
   $file_path = $_FILES['image']['path'][$i];

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.