簡體   English   中英

PHP腳本未定義索引未定義常量和未定義變量

[英]Php script Undefined Index Undefined Constant and Undefined Variable

我正在建立一個具有后端的電子商務,用戶可以在其中使用表單更新產品並將其插入數據庫。 在SO上找到了一些答案但是即使閱讀完草書,我又一次又一次地檢查了我的代碼,但我仍然不知道自己在做什么錯...任何想法嗎? 提交表格后,我收到以下錯誤消息:

Notice: Undefined index: prod_depth in C:\xampp\htdocs\ecommerce\admin\products.php on line 12

Notice: Undefined index: image_1 in C:\xampp\htdocs\ecommerce\admin\products.php on line 17

Notice: Undefined index: image_2 in C:\xampp\htdocs\ecommerce\admin\products.php on line 18

Notice: Undefined index: image_3 in C:\xampp\htdocs\ecommerce\admin\products.php on line 19

Notice: Undefined index: image_4 in C:\xampp\htdocs\ecommerce\admin\products.php on line 20

Notice: Undefined index: prod_depth in C:\xampp\htdocs\ecommerce\admin\products.php on line 27

Notice: Undefined offset: 1 in C:\xampp\htdocs\ecommerce\admin\products.php on line 45

Notice: Use of undefined constant microtime - assumed 'microtime' in C:\xampp\htdocs\ecommerce\admin\products.php on line 55

Notice: Undefined variable: mimeType in C:\xampp\htdocs\ecommerce\admin\products.php on line 63

products.php

<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/ecommerce/core/init.php';
include 'includes/header.php'; 
if(isset($_GET['add'])){
$parentQuery = $db->query("SELECT * FROM categories WHERE parent= 0" );
    if ($_POST) {         
    $name = sanitize($_POST['name']);             
    $categories = sanitize($_POST['child']);         
    $price = sanitize($_POST['price']);        
    $list_price = sanitize($_POST['list_price']);         
    $prod_width = sanitize($_POST['prod_width']); 
                            $prod_depth = sanitize($_POST['prod_depth']);   
                           $prod_height = sanitize($_POST['prod_height']);   
                           $prod_material= sanitize($_POST['prod_material']); 
                           $quantity = sanitize($_POST['quantity']);   
                           $care_instructions= sanitize($_POST['care_instructions']); 
                           $image_1= sanitize($_POST['image_1']); 
                           $image_2= sanitize($_POST['image_2']); 
                           $image_3= sanitize($_POST['image_3']); 
                           $image_4= sanitize($_POST['image_4']); 
    $description = sanitize($_POST['description']);         

    $errors = array();                 
    $required = array('name','child','price','prod_width', 'prod_depth','prod_height', 'prod_material', 'quantity', 'description', 'care_instructions', 'image_1', 'image_2', 'image_3', 'image_4');

                        foreach ($required as $field) {             
    if ($_POST[$field] == '') {                 
    $errors[] = 'All Fields With and Astrisk are required';                 
        break;
        }
        }

        if(!empty($_FILES)){
            var_dump($_FILES);
            $image_1 = $_FILES['image_1'];
            $image_2 = $_FILES['image_2'];
            $image_3 = $_FILES['image_3'];
             $image_4 =$_FILES['image_4'];
             $name = $image_1['name'];
             $name = $image_2['name'];
             $name = $image_3['name'];
             $name = $image_4['name'];
             $nameArray = explode('. ',$name);
             $fileName = $nameArray[0];
             $fileExt =  $nameArray[1];
             $mime =  explode('/',$image_1['type']);
               $mime =  explode('/',$image_2['type']);
                 $mime =  explode('/',$image_3['type']);
                   $mime =  explode('/',$image_4['type']);
             $mimeExt = $mime[1];
             $tmpLoc = $image_1['tmp_name'];
             $fileSize = $image_1['size'];
             $allowed =array('png', 'jpg','jpeg','gif');
             $uploadPath = BASEURL.'/ecommerce/images/products';
             $uploadName = md5(microtime).'.'.$fileExt;
             $dbpath = '/ecommerce/images/products'.$uploadName;
              $tmpLoc = $image_2['tmp_name'];
             $fileSize = $image_2['size'];
              $tmpLoc = $image_3['tmp_name'];
             $fileSize = $image_3['size'];
              $tmpLoc = $image_4['tmp_name'];
             $fileSize = $image_4['size'];
           if ($mimeType != 'image') {                
               $errors[] = 'The file must be an image.';             }
        }
        if(!in_array($fileExt, $allowed)){
            $errors[] = 'The photo extension must be a png, jpg, jpeg or gif';
        }
        if($fileSize >25000000){
            $errors[] = 'The file esize must be under 25 megabytes';
        }
        if($fileExt !=$mimeExt && ($mimeExt == 'jpeg' && $fileExt != 'jpg')){
            $errors[] = 'File extension does not match the';
        }

        if(!empty($errors)){
            echo display_errors($errors);
        }else {             
    //upload file and insert into database            
    move_uploaded_file($tmpLoc, $uploadPath);             
    $insertSql = "INSERT INTO product ('name','child','price','list_price','prod_width', 'prod_depth','prod_height', 'prod_material', 'quantity', 'description', 'care_instructions', 'image_1', 'image_2', 'image_3', 'image_4'); VALUES ('$name','$child','$price','$list_price','$prod_width', '$prod_depth','$prod_height', '$prod_material', '$quantity', '$description', '$care_instructions', '$image_1', '$image_2', '$image_3', '$image_4');";             
    $db->query($insertSql);             
        header('Location: products.php');}}?>
<!--end of query -->

我解決了添加此行的問題:

if (isset($_POST['submit'])) {  

所以現在的代碼是:

<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/ecommerce/core/init.php';
include 'includes/header.php'; 
if(isset($_GET['add'])){
$parentQuery = $db->query("SELECT * FROM categories WHERE parent= 0" );
    if (isset($_POST['submit'])) {      
    $name = sanitize($_POST['name']);             
    $categories = sanitize($_POST['child']);         
    $price = sanitize($_POST['price']);        
    $list_price = sanitize($_POST['list_price']);         
    $prod_width = sanitize($_POST['prod_width']); 
                            $prod_depth = sanitize($_POST['prod_depth']);   
                           $prod_height = sanitize($_POST['prod_height']);   
                           $prod_material= sanitize($_POST['prod_material']); 
                           $quantity = sanitize($_POST['quantity']);   
                           $care_instructions= sanitize($_POST['care_instructions']); 
                           $image_1= sanitize($_POST['image_1']); 
                           $image_2= sanitize($_POST['image_2']); 
                           $image_3= sanitize($_POST['image_3']); 
                           $image_4= sanitize($_POST['image_4']); 
    $description = sanitize($_POST['description']);         

    $errors = array();                 
    $required = array('name','child','price','prod_width', 'prod_depth','prod_height', 'prod_material', 'quantity', 'description', 'care_instructions', 'image_1', 'image_2', 'image_3', 'image_4');

                        foreach ($required as $field) {             
    if ($_POST[$field] == '') {                 
    $errors[] = 'All Fields With and Astrisk are required';                 
        break;
        }
        }

        if(!empty($_FILES)){
            var_dump($_FILES);
            $image_1 = $_FILES['image_1'];
            $image_2 = $_FILES['image_2'];
            $image_3 = $_FILES['image_3'];
             $image_4 =$_FILES['image_4'];
             $name = $image_1['name'];
             $name = $image_2['name'];
             $name = $image_3['name'];
             $name = $image_4['name'];
             $nameArray = explode('. ',$name);
             $fileName = $nameArray[0];
             $fileExt =  $nameArray[1];
             $mime =  explode('/',$image_1['type']);
               $mime =  explode('/',$image_2['type']);
                 $mime =  explode('/',$image_3['type']);
                   $mime =  explode('/',$image_4['type']);
             $mimeExt = $mime[1];
             $tmpLoc = $image_1['tmp_name'];
             $fileSize = $image_1['size'];
             $allowed =array('png', 'jpg','jpeg','gif');
             $uploadPath = BASEURL.'/ecommerce/images/products';
             $uploadName = md5(microtime).'.'.$fileExt;
             $dbpath = '/ecommerce/images/products'.$uploadName;
              $tmpLoc = $image_2['tmp_name'];
             $fileSize = $image_2['size'];
              $tmpLoc = $image_3['tmp_name'];
             $fileSize = $image_3['size'];
              $tmpLoc = $image_4['tmp_name'];
             $fileSize = $image_4['size'];
           if ($mimeType != 'image') {                
               $errors[] = 'The file must be an image.';             }
        }
        if(!in_array($fileExt, $allowed)){
            $errors[] = 'The photo extension must be a png, jpg, jpeg or gif';
        }
        if($fileSize >25000000){
            $errors[] = 'The file esize must be under 25 megabytes';
        }
        if($fileExt !=$mimeExt && ($mimeExt == 'jpeg' && $fileExt != 'jpg')){
            $errors[] = 'File extension does not match the';
        }

        if(!empty($errors)){
            echo display_errors($errors);
        }else {             
    //upload file and insert into database            
    move_uploaded_file($tmpLoc, $uploadPath);             
    $insertSql = "INSERT INTO product ('name','child','price','list_price','prod_width', 'prod_depth','prod_height', 'prod_material', 'quantity', 'description', 'care_instructions', 'image_1', 'image_2', 'image_3', 'image_4'); VALUES ('$name','$child','$price','$list_price','$prod_width', '$prod_depth','$prod_height', '$prod_material', '$quantity', '$description', '$care_instructions', '$image_1', '$image_2', '$image_3', '$image_4');";             
    $db->query($insertSql);             
        header('Location: products.php');}}?>
<!--end of query -->

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM