我正在尝试将图像插入数据库,但是却出现如下错误:
注意:未定义的索引:第18行的C:\ xampp \ htdocs \ inv- \ product_action.php中的product_image 注意:第19行的C:\ xampp \ htdocs \ inv- \ product_action.php中的未定义索引:product_image 注意:未定义的索引:第20行的C:\ xampp \ htdocs \ inv- \ product_action.php中的product_image
<div class="form-group">
<div class="col-md-6">
<label>Material Image:</label>
<input type="file" name="product_image" accept="image/*" class="form-control" required />
</div>
if(isset($_POST['btn_action']))
{
if($_POST['btn_action'] == 'load_brand')
{
echo fill_brand_list($connect, $_POST['category_id']);
}
if($_POST['btn_action'] == 'Add')
{
$imgFile = $_FILES['product_image']['name'];
$tmp_dir = $_FILES['product_image']['tmp_name'];
$imgSize = $_FILES['product_image']['size'];
$upload_dir = 'images/'; // upload directory
$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
// valid image extensions
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
// rename uploading image
$product_image = rand(1000,1000000).".".$imgExt;
// allow valid image file formats
if(in_array($imgExt, $valid_extensions)){
// Check file size '5MB'
if($imgSize < 5000000) {
move_uploaded_file($tmp_dir,$upload_dir.$product_image);
}
else{
$errMSG = "Sorry, your file is too large.";
}
}
else{
$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
$query = "
INSERT INTO product (category_id, brand_id, product_date, product_time, product_name, product_description, product_quantity, product_unit, product_base_price, product_enter_by, product_status, po_no, or_no, vr_no, pc_no, supplier)
VALUES (:category_id, :brand_id, :product_date, :product_time, :product_name, :product_description, :product_quantity, :product_unit, :product_base_price, :product_enter_by, :product_status, :po_no, :or_no, :vr_no, :pc_no, :supplier)
";
$statement = $connect->prepare($query);
$statement->execute(
array(
':category_id' => $_POST['category_id'],
':brand_id' => $_POST['brand_id'],
':product_date' => $_POST["product_date"],
':product_time' => $_POST["product_time"],
':product_name' => $_POST['product_name'],
':product_description' => $_POST['product_description'],
':product_quantity' => $_POST['product_quantity'],
':product_unit' => $_POST['product_unit'],
':product_base_price' => $_POST['product_base_price'],
':po_no' => $_POST['po_no'],
':or_no' => $_POST['or_no'],
':vr_no' => $_POST['vr_no'],
':pc_no' => $_POST['pc_no'],
':supplier' => $_POST['supplier'],
':product_enter_by' => $_SESSION["user_id"],
':product_status' => 'active',
':product_image' => $product_image
)
);
第18行是$imgFile = $_FILES['product_image']['name'];
第19行是$tmp_dir = $_FILES['product_image']['tmp_name'];
第20行是$imgSize = $_FILES['product_image']['size'];
我的PHP产品表格
<?php
//product.php
include('database_connection.php');
include('function.php');
if(!isset($_SESSION["type"]))
{
header('location:login.php');
}
include('header.php');
?>
<span id='alert_action'></span>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading panel-heading-custom">
<div class="row">
<div class="col-lg-10 col-md-10 col-sm-8 col-xs-6">
<h3 class="panel-title">Product Table</h3>
</div>
<div class="col-lg-2 col-md-2 col-sm-4 col-xs-6" align='right'>
<button type="button" name="add" id="add_button" class="btn btn-success btn-xs">Add</button>
</div>
</div>
</div>
<div class="panel-body">
<div class="row"><div class="col-sm-12 table-responsive">
<table id="product_data" class="table table-bordered table-striped">
<thead><tr>
<th>ID</th>
<th>Category</th>
<th>Brand</th>
<th>Product Name</th>
<th>Quantity</th>
<th>Enter By</th>
<th>Status</th>
<th></th>
<th></th>
<th></th>
</tr></thead>
</table>
</div></div>
</div>
</div>
</div>
</div>
<!-- FORM -->
<div id="productModal" class="modal fade">
<div class="modal-dialog">
<form method="post" id="product_form">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"><i class="fa fa-plus"></i> Add Product</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Date</label>
<input type="text" name="product_date" class="form-control" value="<?php echo date("Y-m-d"); ?>" id="date" readonly />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Time</label>
<input type="text" name="product_time" class="form-control" value="<?php date_default_timezone_set("Asia/Hong_Kong"); echo date("h:i:sa"); ?>" id="time" readonly />
</div>
</div>
</div>
<div class="form-group">
<label>Select Category</label>
<select name="category_id" id="category_id" class="form-control" required>
<option value="">Select Category</option>
<?php echo fill_category_list($connect);?>
</select>
</div>
<div class="form-group">
<label>Select Brand</label>
<select name="brand_id" id="brand_id" class="form-control" required>
<option value="">Select Brand</option>
</select>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Enter Product Name</label>
<input type="text" name="product_name" id="product_name" class="form-control" required />
</div>
<div class="form-group">
<div class="col-md-6">
<label>Material Image:</label>
<input type="file" name="product_image" accept="image/*" />
</div>
</div>
</div>
</div>
<div class="form-group">
<label>Product Description</label>
<textarea name="product_description" id="product_description" class="form-control" rows="5" required></textarea>
</div>
<div class="form-group">
<label>Quantity</label>
<div class="input-group">
<input type="text" name="product_quantity" id="product_quantity" class="form-control" required pattern="[+-]?([0-9]*[.])?[0-9]+" />
<span class="input-group-addon">
<select name="product_unit" id="product_unit" required>
<option value="">Select Unit</option>
<option value="Bags">Bags</option>
<option value="Bottles">Bottles</option>
<option value="Box">Box</option>
<option value="Dozens">Dozens</option>
<option value="Feet">Feet</option>
<option value="Gallon">Gallon</option>
<option value="Grams">Grams</option>
<option value="Inch">Inch</option>
<option value="Kg">Kg</option>
<option value="Liters">Liters</option>
<option value="Meter">Meter</option>
<option value="Nos">Nos</option>
<option value="Packet">Packet</option>
<option value="Rolls">Rolls</option>
<option value="Pcs">Pcs</option>
</select>
</span>
</div>
</div>
<div class="form-group">
<label>Price</label>
<input type="text" name="product_base_price" id="product_base_price" class="form-control" required pattern="[+-]?([0-9]*[.])?[0-9]+" />
</div>
<div class="form-group">
<label>Receipt Details</label>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" name="po_no" class="form-control" placeholder="Enter Purhase Order No." id="po_no" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="text" name="or_no" class="form-control" placeholder="Enter Official Receipt No." id="or_no" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="text" name="vr_no" class="form-control" placeholder="Enter Voucher Receipt No." id="vr_no" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="text" name="pc_no" class="form-control" placeholder="Enter Petty Cash No." id="pc_no" />
</div>
</div>
</div>
</div>
<div class="form-group">
<label>Supplier</label>
<select name="supplier" class="form-control" id="supplier" required>
<option value="" >~ Select Supplier ~</option>
<?php
include('connect-db.php');
$query = "SELECT * FROM `supplier`";
$result = mysqli_query($DBcon, $query);
while($row = mysqli_fetch_array($result)):;
?>
<option value="<?php echo $row['supplier_name']; ?>"><?php echo $row['supplier_name']; ?></option>
<?php endwhile; ?>
</select>
</div>
</div>
<div class="modal-footer">
<input type="hidden" name="product_id" id="product_id" />
<input type="hidden" name="btn_action" id="btn_action" />
<input type="submit" name="action" id="action" class="btn btn-info" value="Add" />
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</form>
</div>
</div>
<div id="productdetailsModal" class="modal fade">
<div class="modal-dialog">
<form method="post" id="product_form">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"><i class="fa fa-plus"></i> Product Details</h4>
</div>
<div class="modal-body">
<Div id="product_details"></Div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</form>
</div>
</div>
<!-- FORM -->
<script>
$(document).ready(function(){
var productdataTable = $('#product_data').DataTable({
"processing":true,
"serverSide":true,
"order":[],
"ajax":{
url:"product_fetch.php",
type:"POST"
},
"columnDefs":[
{
"targets":[7, 8, 9],
"orderable":false,
},
],
"pageLength": 10
});
$('#add_button').click(function(){
$('#productModal').modal('show');
$('#product_form')[0].reset();
$('.modal-title').html("<i class='fa fa-plus'></i> Add Product");
$('#action').val("Add");
$('#btn_action').val("Add");
});
$('#category_id').change(function(){
var category_id = $('#category_id').val();
var btn_action = 'load_brand';
$.ajax({
url:"product_action.php",
method:"POST",
data:{category_id:category_id, btn_action:btn_action},
success:function(data)
{
$('#brand_id').html(data);
}
});
});
$(document).on('submit', '#product_form', function(event){
event.preventDefault();
$('#action').attr('disabled', 'disabled');
var form_data = $(this).serialize();
$.ajax({
url:"product_action.php",
method:"POST",
data:form_data,
success:function(data)
{
$('#product_form')[0].reset();
$('#productModal').modal('hide');
$('#alert_action').fadeIn().html('<div class="alert alert-success">'+data+'</div>');
$('#action').attr('disabled', false);
productdataTable.ajax.reload();
}
})
});
$(document).on('click', '.view', function(){
var product_id = $(this).attr("id");
var btn_action = 'product_details';
$.ajax({
url:"product_action.php",
method:"POST",
data:{product_id:product_id, btn_action:btn_action},
success:function(data){
$('#productdetailsModal').modal('show');
$('#product_details').html(data);
}
})
});
$(document).on('click', '.update', function(){
var product_id = $(this).attr("id");
var btn_action = 'fetch_single';
$.ajax({
url:"product_action.php",
method:"POST",
data:{product_id:product_id, btn_action:btn_action},
dataType:"json",
success:function(data){
$('#productModal').modal('show');
$('#category_id').val(data.category_id);
$('#brand_id').html(data.brand_select_box);
$('#brand_id').val(data.brand_id);
$('#product_name').val(data.product_name);
$('#product_description').val(data.product_description);
$('#product_quantity').val(data.product_quantity);
$('#product_unit').val(data.product_unit);
$('#product_base_price').val(data.product_base_price);
$('#product_tax').val(data.product_tax);
$('.modal-title').html("<i class='fa fa-pencil-square-o'></i> Edit Product");
$('#product_id').val(product_id);
$('#action').val("Edit");
$('#btn_action').val("Edit");
}
})
});
$(document).on('click', '.delete', function(){
var product_id = $(this).attr("id");
var status = $(this).data("status");
var btn_action = 'delete';
if(confirm("Are you sure you want to change status?"))
{
$.ajax({
url:"product_action.php",
method:"POST",
data:{product_id:product_id, status:status, btn_action:btn_action},
success:function(data){
$('#alert_action').fadeIn().html('<div class="alert alert-info">'+data+'</div>');
productdataTable.ajax.reload();
}
});
}
else
{
return false;
}
});
});
</script>
<?php include('footer.php'); ?>
答案 0 :(得分:1)
检查表单输入的名称:
<input type="file" name="product_image">
此错误输入名称“ product_image”不存在。
和表单的 enctype :
<form action="" method="post" enctype="multipart/form-data">
答案 1 :(得分:0)
它现在可以正常工作
<form method="post" enctype="multipart/form-data" id="product_form">
这是答案感谢您解决我的问题我的查询很好,所以这是我的真正问题哈哈抱歉,我是初学者