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



我正在使用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 in Line 13

注意:未定义索引:product_img2 in insert_products.php in Line 14

注意:未定义的索引: in insert_products.php in Line 15

注意:未定义的索引:product_img1 in insert_products.php in Line 18

注意:未定义的索引:product_img2 in insert_products.php in Line 19

通知:未定义的索引: in insert_products.php in Line 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">

to

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

然后重试。

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

form-data

上传文件时,始终包含表单属性enctype="multipart/form-data"

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

相关内容

  • 没有找到相关文章

最新更新