尝试上传图像时出现未定义的索引错误

Undefined index error while trying to upload images

我正在使用 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();
            ";
        }
    }
}
?>

正如您所见,一切看起来都很好,但每当我尝试填写表格时,我都会收到以下错误消息:

**

通知:未定义的索引:product_img1 in insert_products.php on line 13

通知:未定义的索引:insert_products.php中的product_img2行14

通知:未定义索引:insert_products.php中的product_img3行15

通知:未定义的索引:insert_products.php中的product_img1行18

通知:未定义的索引:insert_products.php中的product_img2行19

通知:未定义的索引:insert_products.php中的product_img3行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'];

我知道这是一个典型的问题,但我真的很困惑这有什么问题。所以如果你知道,请告诉我.. 谢谢!

更改以下内容:

<form action="" method="POST">

<form action="" method="POST" enctype="multipart/form-data">

然后重试。

要使用 form 上传图片,您必须发送 form-data 编码为 "multipart/form-data"

上传文件时总是包含表单属性enctype="multipart/form-data"

<form action="" method="POST" action="" enctype="multipart/form-data">