<?php 
$result21 = $mysqli->query("SELECT id FROM extras WHERE type=0 AND $photovar='$id' GROUP BY insert_id");
$row = $result21->fetch_array();
$nrrowsp = mysqli_num_rows($result21);
$priorityid=($nrrowsp + 1);
?>
<?php 
if  ( isset($_POST['submitp'.$submitid]) AND ($_FILES['upload']['tmp_name'] != "" ) ) {
$itemid = $mysqli->real_escape_string(stripslashes($_REQUEST['itemid']));

foreach($_FILES['upload']['tmp_name'] as $key => $tmp_name ){

$resultpp = $mysqli->query("SELECT insert_id FROM extras GROUP BY insert_id ORDER BY insert_id desc LIMIT 1");
$row = $resultpp->fetch_array(); 
$isid=$row['insert_id'];
$insertid=$isid + 1;

		$file_name = $key.$_FILES['upload']['name'][$key];
		$file_tmp =$_FILES['upload']['tmp_name'][$key];
		$file_type=$_FILES['upload']['type'][$key];	
		
		if ( $file_tmp != "" ) {
		
	    $mysqli->query("INSERT INTO extras (insert_id, $photovar, priority) VALUES ('$insertid', '$itemid', '$priorityid')");
			
        $result22 = $mysqli->query("SELECT insert_id FROM extras WHERE $photovar='$id' ORDER BY insert_id desc LIMIT 1");
		$row=$result22->fetch_array(); 
		$uploadid=$row['insert_id'];
		
		$result23 = $mysqli->query("SELECT insert_id FROM extras WHERE type=0 AND $photovar='$itemid'");
        $nrrowsp = mysqli_num_rows($result23);
        $priorityid=($nrrowsp + 1);
		
		$uploaddir = $filepath.'photos/';
		$uploadname = $uploadid.'.jpg'; 
	
		copy($file_tmp,$uploaddir.$uploadname);
	
resizephoto($uploaddir.$uploadname,$uploaddir.$uploadname,$thumb_w,$thumb_h);

}}
}

?>

<form name="formap" method="post" enctype="multipart/form-data" onsubmit="displayLoading();">
  <input type="hidden" name="itemid" value="<?php echo $id ?>">
  <input type="hidden" name="max_file_size" value="50000000">
  <input type="hidden" name="search" value="<?php echo $search ?>">
   <table class="tablewidth">
    <tr>
      <td class="tdstyle1 color1<?php if ($nrrowsp>0) { echo " color3"; } ?>"><span class="icon fa-camera"></span> <?php if ($nrrowsp>0) { echo $nrrowsp; } ?> </td>
      <td>
    <input type="file" name="upload[]" size="15" title="upload photos .jpg" multiple>
        </td>
      <td align="right"> <input type="submit" name="submitp<?php echo $submitid ?>" value="<?php echo $adminuploadp ?>" class="button buttonsmall bgrcolor1" title="<?php echo $adminuploadp ?>"></td>
    </tr>
    </table>
</form>
