当我上传文件到phpmyadmin显示4行错误

时间:2017-02-04 07:08:57

标签: php html

我创建此上传表单,用于将产品上传到phpmyadmin,但是当我点击上传按钮时,错误分为4行:

Notice: Undefined index: file in C:\xampp\htdocs\MyShop\admin_area\insert_product.php on line 102
Notice: Undefined index: file in     C:\xampp\htdocs\MyShop\admin_area\insert_product.php on line 103
Notice: Undefined index: file in     C:\xampp\htdocs\MyShop\admin_area\insert_product.php on line 104
Notice: Undefined index: file in     C:\xampp\htdocs\MyShop\admin_area\insert_product.php on line 105

我的HTML是:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>
            Untitled Document
        </title>
    </head>
    <body>
        <form action="insert_product.php" method="post" encypt="multipart/form-data">
            <table align="center" width="800" >
                <tr>
                    <td>
                        Insert New Post
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>
                            Product title:
                        </b>
                    </td>
                    <td>
                        <input type="text" name="product_title" required/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>
                            Product Category:
                        </b>
                    </td>
                    <td>
                        <select name="product_cat">
                            <option>Select a Category</option>
                            <?php
                            $get_cats = "select * from categories";
                            $run_cats = mysqli_query($con, $get_cats);
                            while ($row_cats=mysqli_fetch_array($run_cats)) {
                                $cat_id = $row_cats['cat_id'];
                                $cat_title = $row_cats['cat_title'];
                                echo "<option value='$cat_id'>$cat_title</option>";
                            }
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        <b>
                            Product Image
                        </b>
                    </td>
                    <td>
                        <input type="file" name="file" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>
                            Product Price:
                        </b>
                    </td>
                    <td>
                        <input type="text" name="product_price" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>
                            Product Description:
                        </b>
                    </td>
                    <td>
                        <textarea name="product_desc"></textarea>
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>
                            Product keywords:
                        </b>
                    </td>
                    <td>
                        <input type="text" name="product_keywords" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="submit" name="insert_post" value="Insert Product" />
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>
<?php
if(isset($_POST['insert_post'])) {
    $product_title = $_POST['product_title'];
    $product_cat = $_POST['product_cat'];
    $product_price = $_POST['product_price'];
    $product_desc = $_POST['product_desc'];
    $product_keywords = $_POST['product_keywords'];
    $file = rand(1000,100000)."-".$_FILES['file']['name'];
    $file_loc = $_FILES['file']['tmp_name'];
    $file_size = $_FILES['file']['size'];
    $file_type = $_FILES['file']['type'];
    $folder="uploads/";
    // new file size in KB
    $new_size = $file_size/1024; 
    $new_file_name = strtolower($file);
    // make file name in lower case
    $final_file=str_replace(' ','-',$new_file_name); 
    // new file size in KB
    move_uploaded_file($file_loc,$folder.$final_file);
    $insert_product = "insert into products (product_cat,product_title,file,type,size,,product_price,product_desc,product_keywords) values ('$product_cat','$product_title','$final_file','$file_type','$new_size','$product_price','$product_desc','$product_keywords')";
    $insert_pro = mysqli_query($con, $insert_product);
    if($insert_pro) {
        echo "<script>alert('Product has been inserted!')</script>";
        echo "<script>window.open('insert_product.php','_self')</script>>";
    }
}
?>

2 个答案:

答案 0 :(得分:0)

在对$ _FILES

采取行动之前,您需要检查isset条件
 if(isset($_FILES['file']['name']))
{
 $file = rand(1000,100000)."-".$_FILES['file']['name'];
     $file_loc = $_FILES['file']['tmp_name'];
     $file_size = $_FILES['file']['size'];
     $file_type = $_FILES['file']['type'];
     $folder="uploads/";

     // new file size in KB
     $new_size = $file_size/1024; 
     $new_file_name = strtolower($file);
     // make file name in lower case

     $final_file=str_replace(' ','-',$new_file_name); 
     // new file size in KB

     move_uploaded_file($file_loc,$folder.$final_file);
 }

答案 1 :(得分:0)

您在表单enctype中输入错字:

instr