我已经在代码中使用if(isset($_POST['sub']))
了。但是它在第100行的C:\ xampp \ htdocs \ online shop \ controlar \ product.php中显示注意:未定义的索引:类别。这是一个简单的产品上传页面。当我尝试上传产品时,它可以正常工作,但在类别中不起作用,我的意思是类别未添加到数据库中。
product.php
<?php
require_once("/../dataAccessLayer/dalSession.php");
session::checkA();
require_once("/../dataAccessLayer/dalProduct.php");
require_once("/../dataAccessLayer/dalSub-Category.php");
require_once("/../dataAccessLayer/dalCategory.php");
require_once("/../dataAccessLayer/dalSize.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>Add Product</title>
<link rel="stylesheet" href="../css/contact.css">
<link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Roboto'>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="../js/jquery-1.9.1.min.js"></script>
<script src='../js/jquery.scrollto.js'></script>
</head>
<body>
<?php
if(isset($_GET['action']) && ($_GET['action']== "logout")){
Session::StopA();
}
?>
<nav class="navbar navbar-default navbar-static-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">
Add Product
</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li><a href="../index.php" target="_blank">Visit Site</a> </li>
<li><a href="?action=logout">Logout</a></li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<div class="container-fluid main-container">
<div class="col-md-2 sidebar">
<ul class="nav nav-pills nav-stacked">
<li> <a href="admin.php">Home</a></li>
<li ><a href="category.php">Category Details</a></li>
<li><a href="../controlar/sub-category.php">Sub-Category Details</a></li>
<li><a href="../controlar/size.php">Size Details</a></li>
<li class="active"><a href="../controlar/product.php">Product Details</a></li>
<li><a href="../controlar/service.php">Delivery Charge Details</a></li>
<li><a href="report.php">Sell Report</a></li>
<li><a href="delivary_view.php">Order View</a></li>
<li><a href="member_view.php">Member List</a></li>
</ul>
</div>
<div class="col-md-10 content">
<div class="panel panel-default">
<div class="panel-heading">
<h2 align="center"><b><u>Add New Product</u></b></h2>
</div>
<div class="panel-body">
<h3><a href="product_view.php">View Product</a></h3>
<?php
if (isset($_POST['sub'])){
$pro = new Product();
$pro->p_name = $_POST['name'];
$pro->price = $_POST['price'];
$pro->vat = $_POST['vat'];
$pro->discount = $_POST['discount'];
$pro->picture = $_FILES['pic']['name'];
$pro->details = $_POST['details'];
$pro->quantity = $_POST['quantity'];
$pro->subcat_id = $_POST['subcatagory'];
$pro->cat_id = $_POST['category']; //100 line
$pro->size_id = $_POST['size'];
if ($pro->Insert()) {
echo "Insert Sucessfully";
$image_type = $_FILES['pic']['type'];
if ($image_type=='image/jpeg' || $image_type=='image/jpg' || $image_type=='image/png')
{
move_uploaded_file($_FILES['pic']['tmp_name'],'../image/'.$_FILES['pic']['name']);
}
}
else {
echo "Data Failed";
//echo $_POST['size'];
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<table align="center">
<tr>
<td>Name: </td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Price:</td>
<td><input type="text" name="price" /></td>
</tr>
<tr>
<td>Vat:</td>
<td><input type="text" name="vat" /></td>
</tr>
<tr>
<td>Discount:</td>
<td><input type="text" name="discount" /></td>
</tr>
<tr>
<td>Picture:</td>
<td><input type="file" name="pic" /></td>
</tr>
<tr>
<td>Details:</td>
<td><input type="text" name="details" /></td>
</tr>
<tr>
<td>Quantity:</td>
<td><input type="text" name="quantity" /></td>
</tr>
<tr>
<td>Sub-Category:</td>
<td>
<select name="subcatagory" id="siz">
<?php
$ser = new SubCategory();
$load = $ser->Load();
echo $load;
?>
</select>
</td>
</tr>
<tr>
<td>Category:</td>
<td><div id="cos" name="category"></div></td>
</tr>
<tr>
<td>Size:</td>
<td>
<select name="size">
<?php
$size = new Size();
$size->View_dd();
?>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="sub" value="Save" /></td>
</tr>
</table>
</form>
</div>
</div>
</div>
<hr>
</body>
</html>
<script>
$(document).ready(function(){
$("#siz").change(function(){
var ser_id = $(this).val();
$.ajax({
url:"show_category_delivery.php",
method:"POST",
data:{ser_Id:ser_id},
dataType:"HTML",
success: function(data)
{
$("#cos").html(data);
}
});
});
});
</script>
dalProduct.php
<?php
class Product {
public $product_id;
public $p_name;
public $price;
public $vat;
public $discount;
public $picture;
public $details;
public $quantity;
public $subcat_id;
public $cat_id;
public $size_id;
public function DB()
{
$connection = mysqli_connect("localhost","root","","project");
return $connection;
}
public function SelectById()
{
$sql = "SELECT product.product_id, product.p_name, product.price, product.vat, product.discount, product.picture, product.details, product.quantity, subcategory.name, size.size_name
FROM product,subcategory,size
WHERE product.subcategory_id = subcategory.subcategory_id
AND product.size_id = size.size_id
AND product.product_id = '".$this->product_id."'";
$sql = mysqli_query($this->DB(),$sql);
while($d = mysqli_fetch_row($sql))
{
return $d;
}
}
public function Insert()
{
$sql = "INSERT INTO product (p_name, price, vat, discount, picture, details, quantity, subcategory_id, category_id, size_id)
VALUES ('".$this->p_name."',
'".$this->price."',
'".$this->vat."',
'".$this->discount."',
'".$this->picture."',
'".$this->details."',
'".$this->quantity."',
'".$this->subcat_id."',
'".$this->cat_id."',
'".$this->size_id."');";
if(mysqli_query($this->DB(), $sql))
{
return true;
}
return false;
}
public function Update()
{
$sql = "UPDATE product SET p_name = '".$this->p_name."',
price = '".$this->price."',
vat = '".$this->vat."',
discount = '".$this->discount."',
picture = '".$this->picture."',
details = '".$this->details."',
quantity = '".$this->quantity."',
subcategory_id = '".$this->subcat_id."',
size_id = '".$this->size_id."'
WHERE product_id = '".$this->product_id."'";
if(mysqli_query($this->DB(),$sql))
{
return true;
}
return false;
}
public function View()
{
$sql = "SELECT product.product_id, product.p_name, product.price, product.vat,
product.discount, product.picture, product.details, product.quantity,
subcategory.name, size.size_name
FROM product,subcategory,size
WHERE product.subcategory_id = subcategory.subcategory_id AND
product.size_id = size.size_id";
$sql_connect = mysqli_query($this->DB(),$sql);
while($d = mysqli_fetch_row($sql_connect))
{
$arr[] = $d;
}
return $arr;
}
public function Delete()
{
$sql = "DELETE FROM product WHERE product_id = '".$this->product_id."'";
if(mysqli_query($this->DB(),$sql))
{
return true;
}
return false;
}
}
?>
最佳答案
我给您其他建议:
只需添加以下代码并手动输入类别ID。
<td>Category:</td>
<td><div id="cos"></div> <input type="text" id="category" name="category" /></td>