2016-11-19 120 views
0

我正在构建一个后端电子商务,用户可以使用表单在数据库中上传和插入产品。我发现了一些答案here on SO但是即使在阅读了微软的消息之后,我一次又一次地浏览了我的代码,但我仍然无法弄清楚我做错了什么......任何想法?在提交表单我收到这些错误:PHP脚本未定义的索引未定义的常量和未定义的变量

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 --> 
+0

[PHP:“Notice:Undefined variable”和“Notice:Undefined index”]的可能重复(http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Qirel

+0

你确定$ _POST变量实际上是由用户提交的表单设置的吗?你确定请求方法实际上是POST而不是GET吗? – 2016-11-19 19:56:04

+1

@Hallur:用行解决问题if(isset($ _ POST ['submit'])){ –

回答

0

我解决了这一问题与加入这一行:

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 -->