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 -->
[PHP:「Notice:Undefined variable」和「Notice:Undefined index」]的可能重複(http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Qirel
你確定$ _POST變量實際上是由用戶提交的表單設置的嗎?你確定請求方法實際上是POST而不是GET嗎? – 2016-11-19 19:56:04
@Hallur:用行解決問題if(isset($ _ POST ['submit'])){ –