我正在构建一个带有后端的电子商务,用户可以使用表单升级并在数据库中插入产品。我找到了一些答案here on 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 -->
答案 0 :(得分: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 -->