Hi,
Appologies for my poor english
I’m new to programming, im learning in the web, im doing a project to develop an online store, so basically I created a function to update product details,
I created an automatic form to update product details, however, when I print_r($_FILES), it shows an empty array even though I submit files on it.
My code is below:
<?php
function product_settings($all_products){
global $con;
$inputs_required = array('product_id','product_title','product_cat' ,
'product_brand','product_image','product_price','product_desc',
'product_keywords','product_status',
);
$inputs = array(
'product_image' => 'Product Image ',
'product_id' => 'Product ID ',
'product_title' => "Product title ",
'product_cat' => "Product Category ",
'product_brand' => 'Product Brand ',
'product_price' => 'Product Price ',
'product_desc' => 'Product Description ',
'product_keywords' => "Product Keywords ",
'product_status' => "Status ",
);
$non_changable_det = array('product_id','user_id');
echo '
<form action= "../admin_area/products.php?cat=3" method = "POST" enctype= "multipart/form-data">
';
echo '<div id="products_box">';
foreach($all_products as $key => $data){
echo '
<div id = "single_product">
<table>
';
foreach($data as $key1 => $data1){
if(in_array($key1, $inputs_required )){
echo '<tr>';
if ($key1 == 'product_image'){
echo '<th>'.$inputs[$key1].':
<p>Change image:</p>
</th>
<td><img src = "../'.$data['product_image'].' " width ="180px" height = "150px">
<p><input type ="file" name = "'.$key.'['.$key1.']" /> </p>
</td>
';
}else if($key1 == 'product_id'){
echo '
<th>'.$inputs[$key1].':</th>
<td>'.$data1.'</td>
';
}else if($key1 == 'product_desc'){
echo '
<th>'.$inputs[$key1].':</th>
<td><textarea name="'.$key.'['.$key1.']" col = "20" rows = "10" >'.$data1.'</textarea></td>
';
}else if($key1 == 'product_cat'){
// $all_categories = getCats(); takes all categories from the database
$all_categories = (
'cat_id_1' => Array
(
'cat_id' => 1,
'cat_title' => 'Laptops',
),
'cat_id_2' => Array
(
'cat_id' => 2,
'cat_title' => 'Cameras',
),
'cat_id_3' => Array
(
'cat_id' => 3,
'cat_title' => 'Mobile',
),
'cat_id_4' => Array
(
'cat_id' => 4,
'cat_title' => 'Computers',
),
);
echo '
<th>'.$inputs[$key1].':</th>
<td><select name = "'.$key.'['.$key1.']">
';
foreach( $all_categories as $k => $d){
if( $d['cat_id'] == $data1){
echo '<option value = "'.$d['cat_id'].'" selected>'.$d['cat_title'].' </option>';
}else{
echo '<option value = "'.$d['cat_id'].'" >'.$d['cat_title'].' </option>';
}
}
echo'
</select></td>
';
}else if($key1 == 'product_brand'){
//$all_brands = getBrands(); takes all categories from the database
$all_brands = (
['brand_id_1' => Array
(
'brand_id' => 1,
'brand_title' => 'HTC',
)
'brand_id_2' => Array
(
'brand_id' => 2,
'brand_title' => 'Samsung',
),
'brand_id_3' => Array
(
'brand_id' => 3,
'brand_title' => 'Apple',
),
'brand_id_4' => Array
(
'cat_id' => 4,
'cat_title' => 'HP',
),
);
echo '
<th>'.$inputs[$key1].':</th>
<td><select name ="'.$key.'['.$key1.']">
';
foreach( $all_brands as $k => $d){
if( $d['brand_id'] == $data1){
echo '<option value = "'.$d['brand_id'].'" selected>'.$d['brand_title'].' </option>';
}else{
echo '<option value = "'.$d['brand_id'].'" >'.$d['brand_title'].' </option>';
}
}
echo'
</select></td>
';
}else if($key1 == 'product_status'){
echo '
<th>'.$inputs[$key1].':</th>
<td> <select name = "'.$key.'['.$key1.']">
';
if ($data1 == 0){
echo '<option value = "'.$data1.'" selected > Not Active</option>
<option value = "1" > Active</option>
';
}else if($data1 == 1){
echo '<option value = "0" > Not Active</option>
<option value = "'.$data1.'" selected > Active</option>
';
}
echo '</select></td>
';
}else{
echo '
<td>'.$inputs[$key1].':
<input type="text" name = "'.$key.'['.$key1.']" value = "'.$data1.'" /> </td>
';
}
echo '</tr>';
}
}//end foreach($data as $key1 => $data1)
echo '</table></div>'; // end of <div id = "single_product">
}//foreach($all_products as $key => $data)
echo '
</div>'; // end <div id="products_box">
echo '
<input type="submit" name = "update" value= "Update details">
</form>
';
}// end function product_settings($all_products)
// script call functions
$all_products = array(
'product_id_10' => Array
(
'product_id' => 10,
'product_title' => 'block',
'product_cat' => 2,
'product_brand' => 3,
'product_price' => 68,
'product_desc' => 'block',
'product_image' => 'admin_area/product_images/6723e937dee80bbfa3cec44506f6c33e.PNG',
'product_keywords' => 'block',
'user_id' => 0,
'product_status' => 0,
),
'product_id_9' => Array
(
'product_id' => 9,
'product_title' => 'house',
'product_cat' => 1,
'product_brand' => 1,
'product_price' => 16,
'product_desc' => 'this is a house',
'product_image' => 'admin_area/product_images/813f1ce7b2c0597b0886e0d393b2c4fa.PNG',
'product_keywords' => 'house',
'user_id' => 0,
'product_status' => 0,
),
);
if(isset($_POST)){
//$post_variable = $_POST;
$image_files = $_FILES;
print_r($image_files);
}
product_settings($all_products);
?>