php错误甚至在定义变量之后

时间:2018-04-29 23:48:34

标签: php html mysql

这是我的项目插入产品的代码块

import java.lang.instrument.Instrumentation;

public class Agent {
  public static void premain(String args, Instrumentation instrumentation){
    // early initalization
  }
}

即使在定义变量后我得到错误未定义索引:产品编号在C:\ xampp \ htdocs \ RangProject \ admin_area \ insert_products.php第183行 这是我的第183行

<?php
include('includes/db.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html lang="EN">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="shortcut icon" href="images/logo.jpg" type="image/x-icon" />
<title>Insert Products</title>
<script src="https://cloud.tinymce.com/stable/tinymce.min.js"></script>
  <script>tinymce.init({ selector:'textarea' });</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap.min.css" />
<link rel="stylesheet" href="fontawesome/css/font-awesome.min.css" />
</head>
<body>

<div class="row">
<div class="col-lg-12">
 <ol class="breadcrumb">
<li class="active"><i class="fa fa-dashboard"></i>Dashboard/Insert Products</li>   

</ol> <!--breadcrumb ends here-->  
 </div> <!---col-lg-12 ends-->    
</div> <!--row ends-->


<div class="row">
<div class="col-lg-12">
 <div class="panel panel-default">
<div class="panel-heading">
 <h3 class="panel-title">
 <i class="fa fa-money fa-fw"></i> Insert Products  
</h3>    

</div> <!--panel heading ends--->    

<div class="panel-body">
 <form class="form-horizontal" method="post" enctype="multipart/form-data">
 <div class="form-group">
<label class="col-md-3 control-label" >Product title</label>
<div class="col-md-6">
<input type="text" name="product_title" class="form-control" required>      

</div>

</div> <!---form-group-->   

 <div class="form-group">
<label class="col-md-3 control-label" >Product Category</label>
<div class="col-md-6">
    <select name="product_cat" class="form-control" required>
<option>Select Category</option> 
<?php
$get_p_cats="select * from product_categories";
$run_p_cats=mysqli_query($conn,$get_p_cats);

while($row_p_cats=mysqli_fetch_array($run_p_cats)){
    $p_cat_id=$row_p_cats['p_cat_id'];
    $p_cat_title=$row_p_cats['p_cat_title'];

    echo "<option value='$p_cat_id'>$p_cat_title</option>";
}

?>


</select>      

</div>

</div> <!---form-group-->   


 <div class="form-group">
<label class="col-md-3 control-label" >Category</label>
<div class="col-md-6">
<select name="cat" class="form-control" required>     
<option>Select Category</option> 
<?php
$get_cat="select * from categories";
$run_cat=mysqli_query($conn,$get_cat);

while($row_cat=mysqli_fetch_array($run_cat)){
    $cat_id=$row_cat['cat_id'];
    $cat_title=$row_cat['cat_title'];

    echo "<option value='$cat_id'>$cat_title</option>";
}

?>  

    </select>
</div>

</div> <!---form-group-->   



 <div class="form-group">
<label class="col-md-3 control-label" >Product Image 1</label>
<div class="col-md-6">
<input type="file" name="product_img1" class="form-control" required>      

</div>

</div> <!---form-group-->   


 <div class="form-group">
<label class="col-md-3 control-label" >Product Image 2</label>
<div class="col-md-6">
<input type="file" name="product_img2" class="form-control" required>      

</div>

</div> <!---form-group-->   


<div class="form-group">
<label class="col-md-3 control-label" >Product Image 3</label>
<div class="col-md-6">
<input type="file" name="product_img3" class="form-control" required>      

</div>
 </div> <!---form-group--> 

  <div class="form-group">
<label class="col-md-3 control-label" >Product Price</label>
<div class="col-md-6">
<input type="text" name="product_price" class="form-control" required>      

</div>
 </div> <!---form-group-->    

 <div class="form-group">
<label class="col-md-3 control-label" >Product Keywords</label>
<div class="col-md-6">
<input type="text" name="product_keywords" class="form-control" required>      

</div>
 </div> <!---form-group-->  


 <div class="form-group">
<label class="col-md-3 control-label" >Product Description</label>
<div class="col-md-6">
<textarea name="product_desc" class="form-control" rows="6" cols="19" style="max-width:100%; max-height:100%;"></textarea>      

</div>
 </div> <!---form-group-->  


 <div class="form-group">
<label class="col-md-3 control-label" ></label>
<div class="col-md-6">
<input type="submit" name="submit" value="Insert Product" class="btn btn-primary form-control">     
</div>
 </div> <!---form-group-->

</form>   <!--form-horizontal ends--->

</div> <!--panel-body ends-->

</div>  <!---panel panel-default ends--> 

</div> <!--col-lg-12 ends--->    

</div>  <!--row 2 ends-->
</body>
</html>

<?php

if(isset($_POST['submit'])){
    $product_title=$_POST['product_title'];
    $product_cat=$_POST['product_cat'];
    $cat=$_POST['cat'];
    $product_price=$_POST['product_price'];
    $product_desc=$_POST['product_desc'];
    $product_keyword=$_POST['product_keywords'];

    $product_img1=$_FILES['product_img1']['name'];
    $product_img2=$_FILES['product_img2']['name'];
    $product_img3=$_FILES['product_img3']['name'];

    $temp_name1=$_FILES['product_img1']['tmp_name'];
    $temp_name2=$_FILES['product_img2']['tmp_name'];
    $temp_name3=$_FILES['product_img3']['tmp_name'];

    move_uploaded_file($temp_name1,"product_images/$product_img1");
    move_uploaded_file($temp_name2,"product_images/$product_img2");
    move_uploaded_file($temp_name3,"product_images/$product_img3");

$insert_product="insert into products(p_cat_id,cat_id,date,product_title,product_img1,product_img2,product_img3,product_price,product_desc,product_keywords) values( '$product_cat','$cat',NOW(),'$product_title','$product_img1','$product_img2','$product_img3','$product_price','$product_desc','$product_keyword')";


$run_product=mysqli_query($conn,$insert_product);

if($run_product){
    echo "<script>alert('Product has been inserted successfully')</script>";
    echo "<script>window.open('insert.php','self')</script>";
}
}
?>

谁能告诉我我的错误是什么? Php非常混乱且区分大小写。

3 个答案:

答案 0 :(得分:0)

在分配之前检查您的帖子是否为空。

$product_price = isset($_POST['product_price']) ? $_POST['product_price'] : '';

答案 1 :(得分:0)

这是因为$ _POST上不存在“product_price”。

要避免此错误,请先使用isset()检查它是否存在:

$product_price = isset($_POST['product_price']) ? $_POST['product_price'] : '';

答案 2 :(得分:0)

修正了我的错误。刚从我收到的所有回复中得到一些建议之后添加了这个。谢谢大家。

$product_title=mysqli_real_escape_string($conn,$_POST['product_title']);
    $product_cat=mysqli_real_escape_string($conn,$_POST['product_cat']);
    $cat=mysqli_real_escape_string($conn,$_POST['cat']);
    $product_price =mysqli_real_escape_string($conn,$_POST['product_price']);
    $product_desc=mysqli_real_escape_string($conn,$_POST['product_desc']);
    $product_keyword=mysqli_real_escape_string($conn,$_POST['product_keywords']);