我正在制作插入表单以添加产品到mysql数据库。我正在使用echo命令打印查询。打印的查询显示如下所示的空值:
注意:未定义的索引:product_cat在F:'Apache24'htdocs'Site1'admin'product_add.php在第40行
注意:未定义索引:product_brand inF:'Apache24'htdocs'Site1'admin'product_add.php on line 41
注意:未定义索引:product_title在F:'Apache24'htdocs'Site1'admin'product_add.php第42行
注意:未定义索引:product_price inF:'Apache24'htdocs'Site1'admin'product_add.php on line 43
注意:未定义索引:product_description inF:'Apache24'htdocs'Site1'admin'product_add.php on line 44
注意:未定义索引:product_keywords inF:'Apache24'htdocs'Site1'admin'product_add.php on line 45
注意:未定义索引:product_images inF:'Apache24'htdocs'Site1'admin'product_add.php on line 48
注意:未定义索引:product_images inF:'Apache24'htdocs'Site1'admin'product_add.php on line 49
insert into products ('product_cat', 'product_brand', 'product_title','product_price', 'product_desc', 'product_image', 'product_keywords')Values (", ", ", ", ", ", ")
这是html表单。它也是动作页:
<!DOCTYPE html>
<html>
<head>
<title>Admin Panel - Add Products</title>
<link rel="stylesheet" type="text/css" href="../styles/admin-style.css">
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
<script>tinymce.init({ selector:'textarea' });</script>
<?php
include("../includes/connect.php");
function getcats_add_products(){
global $con;
$get_cats="select * from categories";
$run_cats=mysqli_query($con, $get_cats);
echo "<option>Select Category</option>";
while($row_cats = mysqli_fetch_array($run_cats)){
$cat_id = $row_cats['cat_id'];
$cat_title = $row_cats['cat_title'];
echo "<option value='$cat_id'>$cat_title</option>";
}
}
function getbrands_add_products(){
global $con;
$get_brands="select * from brands";
$run_brands=mysqli_query($con, $get_brands);
echo "<option>Select Brand</option>";
while($row_brands = mysqli_fetch_array($run_brands)){
$brand_id = $row_brands['brand_id'];
$brand_title = $row_brands['brand_title'];
echo "<option value='$brand_id'>$brand_title</option>";
}
}
if(isset($_POST['submit'])){
$product_cat = $_POST['product_cat'];
$product_brand = $_POST['product_brand'];
$product_title = $_POST['product_title'];
$product_price = $_POST['product_price'];
$product_desc = $_POST['product_description'];
$product_keywords = $_POST['product_keywords'];
$product_images = $_FILES['product_images']['name'];
$product_images_temp = $_FILES['product_images']['tmp_name'];
$product_query = "insert into products
('product_cat',
'product_brand',
'product_title',
'product_price',
'product_desc',
'product_image',
'product_keywords')
values(
'$product_cat',
'$product_brand',
'$product_title',
'$product_price',
'$product_desc',
'$product_images',
'$product_keywords') ";
echo $product_query;
}
?>
</head>
<body>
<div class="wrapper">
<header>
</header>
<div class="heading">Add New Product</div>
<div class="product-table-div">
<form method="POST" action="" enctype="multipart/form-data">
<table class="product-table" border="1">
<tr>
<td id="product-add-label">Product Category</td>
<td>
<select id="product-table-input" name="product-cat">
<?php getcats_add_products(); ?>
</select>
</td>
</tr>
<tr>
<td id="product-add-label">Product Brand</td>
<td>
<select id="product-table-input" name="product-brand">
<?php getbrands_add_products(); ?>
</select>
</td>
</tr>
<tr>
<td id="product-add-label">Product title</td>
<td>
<input type="text" name="product-title" id="product-table-input">
</td>
</tr>
<tr>
<td id="product-add-label">Product Price</td>
<td>
<input type="number" name="product-price" id="product-table-input">
</td>
</tr>
<tr>
<td id="product-add-label">Product description</td>
<td>
<textarea rows="10" cols="30" name="product-description"></textarea>
</td>
</tr>
<tr>
<td id="product-add-label">Product image</td>
<td>
<input type="file" name="product-images" id="product-table-input">
</td>
</tr>
<tr>
<td id="product-add-label">Product Keywords</td>
<td>
<input type="text" name="product-keywords" id="product-table-input">
</td>
</tr>
<tr>
<td colspan="2">
<div id="product-submit-div">
<input type="reset" name="submitreset" id="product-submit" value="Clear">
<input type="submit" name="submit" id="product-submit" value="Add">
</div>
</td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
这个页面的位置是root>admin>product_add.php。有什么问题吗?哦,我还在文本区使用了tinymce文本编辑器
您在HTML表单中使用连字符-
符号作为字段名称;但是当你尝试在PHP中读取时,你使用的是下划线。
$product_cat = $_POST['product_cat'];
$product_brand = $_POST['product_brand'];
$product_title = $_POST['product_title'];
$product_price = $_POST['product_price'];
$product_desc = $_POST['product_description'];
$product_keywords = $_POST['product_keywords'];
用下面的代码代替上面的
$product_cat = $_POST['product-cat'];
$product_brand = $_POST['product-brand'];
$product_title = $_POST['product-title'];
$product_price = $_POST['product-price'];
$product_desc = $_POST['product-description'];
$product_keywords = $_POST['product-keywords'];