我正在使用PHP制作基本CMS来管理我的在线商店。所以我创建了一个页面,用户可以使用自定义图像和信息插入新产品。这是insert_product.php
页面:
<div class="box-body">
<form action="" method="POST">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<div class="form-group">
<label>Product Title:</label>
<input type="text" name="product_title" class="form-control my-colorpicker1">
</div>
</div>
<div class="form-group">
<label>Product Category:</label>
<select class="form-control select2" name="product_cat" style="width: 100%;">
<?php
echo get_cats();
?>
</select>
</div>
<div class="form-group">
<label>Product Brand:</label>
<select class="form-control select2" name="product_brand" style="width: 100%;">
<?php
echo get_brands();
?>
</select>
</div>
<div class="form-group">
<div class="form-group">
<label>Product Image 1:</label>
<input type="file" name="product_img1" class="form-control my-colorpicker1">
</div>
</div>
<div class="form-group">
<div class="form-group">
<label>Product Image 2:</label>
<input type="file" name="product_img2" class="form-control my-colorpicker1">
</div>
</div>
<div class="form-group">
<div class="form-group">
<label>Product Image 3:</label>
<input type="file" name="product_img3" class="form-control my-colorpicker1">
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<div class="form-group">
<label>Product Price:</label>
<input type="text" name="product_price" class="form-control my-colorpicker1">
</div>
</div>
<div class="form-group">
<div class="form-group">
<label>Product Keywords:</label>
<input type="text" name="product_keywords" class="form-control my-colorpicker1">
</div>
</div>
<input type="submit" name="insert_product" class="btn" value="Submit">
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="box box-info">
<div class="box-header">
<h3 class="box-title">Product Description:
<small>Add new product to your onlinestore</small>
</h3>
<div class="pull-right box-tools">
<button type="button" class="btn btn-info btn-sm" data-widget="collapse" data-toggle="tooltip" title="Collapse">
<i class="fa fa-minus"></i></button>
<button type="button" class="btn btn-info btn-sm" data-widget="remove" data-toggle="tooltip" title="Remove">
<i class="fa fa-times"></i></button>
</div>
</div>
<div class="box-body pad">
<textarea name="product_desc" placeholder="Add description to your product" style="width: 100%; height: 200px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;"></textarea>
</div>
</div>
</div>
</div>
</form>
</div>
这个动作文件是这样的:
<?php
if(isset($_POST['insert_product'])){
// text data variables
$product_title = $_POST['product_title'];
$product_cat = $_POST['product_cat'];
$product_brand = $_POST['product_brand'];
$product_price = $_POST['product_price'];
$product_desc = $_POST['product_desc'];
$status = 'on';
$product_keywords = $_POST['product_keywords'];
// image names
$product_img1 = $_FILES['product_img1']['name'];
$product_img2 = $_FILES['product_img2']['name'];
$product_img3 = $_FILES['product_img3']['name'];
// image temp names
$temp_name1 = $_FILES['product_img1']['tmp_name'];
$temp_name2 = $_FILES['product_img2']['tmp_name'];
$temp_name3 = $_FILES['product_img3']['tmp_name'];
if($product_title == '' OR $product_cat == '' OR $product_brand == '' OR $product_price == '' OR $product_desc == '' OR $product_keywords == '' OR $product_img1 == ''){
echo "
<script>alert('Please fill all the fields!')</script>
exit();
";
}else{
// uploading images to its folder
move_uploaded_file($temp_name1,"product_images/$product_img1");
move_uploaded_file($temp_name2,"product_images/$product_img2");
move_uploaded_file($temp_name3,"product_images/$product_img3");
$insert_product = "
INSERT INTO products (cat_id,brand_id,date,product_title,product_img1,product_img2,product_img3,product_price,product_desc,status)
VALUES ('$product_cat','$product_brand',NOW(),'$product_title','$product_img1','$product_img2','$product_img3','$product_price','$product_desc','$status')
";
$run_product = mysqli_query($con,$insert_product);
if($run_product){
echo "
<script>alert('Product inserted successfully')</script>
exit();
";
}
}
}
?>
因为你可以看到一切看起来很好但是每当我尝试填写表单时,我都会收到以下错误消息:
**
注意:插入产品。 13 中未定义的索引:product_img1
注意:插入产品。 14 中未定义的索引:product_img2
注意:行 15 insert_products.php 中的未定义索引:product_img3
注意:插入产品。 18 中未定义的索引:product_img1
注意:插入产品。 19 中未定义的索引:product_img2
通知:插入产品。 20
**
这与以下几行有关:
// image names
$product_img1 = $_FILES['product_img1']['name'];
$product_img2 = $_FILES['product_img2']['name'];
$product_img3 = $_FILES['product_img3']['name'];
// image temp names
$temp_name1 = $_FILES['product_img1']['tmp_name'];
$temp_name2 = $_FILES['product_img2']['tmp_name'];
$temp_name3 = $_FILES['product_img3']['tmp_name'];
我知道这是一个典型的问题,但我真的很困惑,这有什么不妥。所以,如果你知道,请让我..谢谢!
答案 0 :(得分:3)
更改以下内容:
<form action="" method="POST">
到
<form action="" method="POST" enctype="multipart/form-data">
再试一次。
要使用form
上传图片,您必须发送form-data
编码为"multipart/form-data"
答案 1 :(得分:0)
上传文件时始终包含表单属性enctype="multipart/form-data"
。
<form action="" method="POST" action="" enctype="multipart/form-data">