PHP错误,甚至在定义变量后 [英] php error and even after defining variable

查看:78
本文介绍了PHP错误,甚至在定义变量后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

pre $ <?php
include('包括/ db.php中');
?>

<!DOCTYPE html PUBLIC - // W3C // DTD HTML 4.0 Strict // EN>
< html lang =EN>
< head>
< meta name =viewportcontent =width = device-width,initial-scale = 1>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< link rel =快捷图标href =images / logo.jpgtype =image / x-icon/>
< title>插入产品< / 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 =stylesheethref =https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css>
< link rel =stylesheethref =css / bootstrap.min.css/>
< link rel =stylesheethref =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> <! - 面包屑到此为止 - >
< / div> <!--- col-lg-12 ends - >
< / div> <! - 行结束 - >


< 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>插入产品
< / h3>

< / div> <! - 面板标题结束--->

< div class =panel-body>
< form class =form-horizo​​ntalmethod =postenctype =multipart / form-data>
< div class =form-group>
< label class =col-md-3 control-label>产品标题< / label>
< div class =col-md-6>
< input type =textname =product_titleclass =form-controlrequired>

< / div>

< / div> < ---形式的基团 - >!;

< div class =form-group>
< label class =col-md-3 control-label>产品类别< / label>
< div class =col-md-6>
< select name =product_catclass =form-controlrequired>
< option>选择类别< / 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> < ---形式的基团 - >!;


< div class =form-group>
< label class =col-md-3 control-label> Category< / label>
< div class =col-md-6>
< select name =catclass =form-controlrequired>
< option>选择类别< / 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> < ---形式的基团 - >!;



< div class =form-group>
< label class =col-md-3 control-label>产品图片1< / label>
< div class =col-md-6>
< input type =filename =product_img1class =form-controlrequired>

< / div>

< / div> < ---形式的基团 - >!;


< div class =form-group>
< label class =col-md-3 control-label>产品图片2< / label>
< div class =col-md-6>
< input type =filename =product_img2class =form-controlrequired>

< / div>

< / div> < ---形式的基团 - >!;


< div class =form-group>
< label class =col-md-3 control-label>产品图片3< / label>
< div class =col-md-6>
< input type =filename =product_img3class =form-controlrequired>

< / div>
< / div> < ---形式的基团 - >!;

< div class =form-group>
< label class =col-md-3 control-label>产品价格< / label>
< div class =col-md-6>
< input type =textname =product_priceclass =form-controlrequired>

< / div>
< / div> < ---形式的基团 - >!;

< div class =form-group>
< label class =col-md-3 control-label>产品关键字< / label>
< div class =col-md-6>
< input type =textname =product_keywordsclass =form-controlrequired>

< / div>
< / div> < ---形式的基团 - >!;


< div class =form-group>
< label class =col-md-3 control-label>产品描述< / label>
< div class =col-md-6>
< textarea name =product_descclass =form-controlrows =6cols =19style =max-width:100%; max-height:100%;><< ; / textarea的>

< / div>
< / div> < ---形式的基团 - >!;


< div class =form-group>
< label class =col-md-3 control-label>< / label>
< div class =col-md-6>
< input type =submitname =submitvalue =Insert Productclass =btn btn-primary form-control>
< / div>
< / div> < ---形式的基团 - >!;

< / form> <! - 表单横向结束--->

< / div> <! - 面板主体结束 - >

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

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

< / div> <! - 行2结束 - >
< / body>
< / html>



$ b 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); ('$ product_cat','$ cat',NOW($ p
$ b $ 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) ), '$ PRODUCT_TITLE', '$ product_img1', '$ product_img2', '$ product_img3', '$ PRODUCT_PRICE', '$ product_desc', '$ product_keyword');


$ run_product = mysqli_query($ conn,$ insert_product);
$ b $ if if($ run_product){
echo< script> alert('Product has been inserted'')< / script>;
echo< script> window.open('insert.php','self')< / script>;
}
}
?>

即使在定义变量后,我也会收到错误Undefined index:product_price in C:\xampp\htdocs\\ \\ RowProject \admin_area\insert_products.php on line 183
这是我的行183

  $ product_price = $ _POST [ 'PRODUCT_PRICE']; 

有人可以告诉我我的错误是什么? Php是相当混乱和区分大小写。

解决方案

这是因为product_price不存在于$ _POST。



为避免此错误,请使用 isset()

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


this is my block of code for insert product of my project

<?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>";
}
}
?>

Even after defining variable i get error Undefined index: product_price in C:\xampp\htdocs\RangProject\admin_area\insert_products.php on line 183 this is my line 183

 $product_price=$_POST['product_price'];

can someone tell me what my mistake is?? Php is quite confusing and case sensitive.

解决方案

This happens because "product_price" does not exist on $_POST.

To avoid this error, check if it exists first using isset():

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

这篇关于PHP错误,甚至在定义变量后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆