简体   繁体   English

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

[英]when i upload file to phpmyadmin show error in 4 lines

I create this upload form for uploading products to phpmyadmin but when I click on upload button its error in 4 lines: 我创建了此上传表单,用于将产品上传到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

My html is: 我的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>>";
    }
}
?>

You need to check isset condition before taking action on $_FILES 在对$ _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);
 }

您在表单enctype中有错字错误:

<form action="insert_product.php" method="post" enctype="multipart/form-data">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM