我已阅读所有相关文章,但未找到我的解决方案。 实际上我正在创建一个电子商务网站,并在我创建一个HTML(PHP)表单以将数据添加到Mysql(数据库)后在My Project中创建Admin-Panel。每次都会出现以下错误。
"注意:该表单中特定行的未定义索引错误"
我的代码在这里
<?php
include ("includes/db.php");
?>
<html>
<head>
</head>
<body>
<div id="form">
<form method="post" action="insert_product.php" enctype="multipart/form-date">
<table width="1200" align="center">
<tr>
<td> <h2> Insert a New Product</h2> </td>
<tr>
<td><input type="text" style="width:450px; height:30px; border:2px solid black;" placeholder="Enter Product Title" name="product_title"> </td>
</tr>
<tr>
<td>
<select style="width:450px; height:30px; border:2px solid black;" name="product_cart" >
<option> Select a Category</option>
<?php
$get_cats = "select * from categories";
$run_cats = mysqli_query ($con, $get_cats);
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>";
}
?>
</td>
</tr>
<tr>
<td> <Select style="width:450px; height:30px; border:2px solid black;" name="product_brand">
<option> Select a Brand </option>
<?php
$get_brands = "select * from brands";
$run_brands = mysqli_query ($con, $get_brands);
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>";
}
?>
</td>
</tr>
<tr>
<td> <input type="file" style="width:450px; height:30px; border:2px solid blue;" name="product_img1"> </td>
</tr>
<tr>
<td> <input type="file" style="width:450px; height:30px; border:2px solid blue;" name="product_img2"> </td>
</tr>
<tr>
<td> <input type="file" style="width:450px; height:30px; border:2px solid blue;" name="product_img3"> </td>
</tr>
<tr>
<td> <input type="text" style="width:450px; height:30px; border:2px solid blue;" placeholder="Enter Product Price" name="product_price"> </td>
</tr>
<tr>
<td><input type="text" style="width:450px; height:30px; border:2px solid blue;" placeholder="Enter Product Descrption" name="product_desc"> </td>
</tr>
<tr>
<td><input type="text" style="width:450px; height:30px; border:2px solid blue;" placeholder="Enter Product Keywords" name="product_product_keywords"> </td>
</tr>
<tr>
<td> <input type="submit" style="width:452px; height:30px; border:2px solid blue;" name="insert_product" value="Add Product"> </td>
</tr>
</form>
<?php
if (isset ($_POST['insert_product'])) {
//for text adding we can use following code
$product_title= $_POST ['product_title'];
$product_cat= $_POST ['product_cat'];
$product_brand= $_POST ['product_brand'];
$product_desc= $_POST ['product_desc'];
$product_price= $_POST ['product_price'];
$status= 'on';
$product_keywords= $_POST ['product_keywords'];
//for images we have to use following code
$product_img1= $_FILES ['product_img1'] ['name'];
$product_img2= $_FILES ['product_img2'] ['name'];
$product_img3=$_FILES ['product_img3'] ['name'];
//for images temporary names
$temp_img1= $_FILES ['product_img1'] ['temp_name'];
$temp_img2= $_FILES ['product_img1'] ['temp_name'];
$temp_img3= $_FILES ['product_img1'] ['temp_name'];
if($product_title=='' OR $product_cat=='' OR $product_brand=='' OR $product_desc=='' OR $product_price=='' OR $product_keywords=='' OR $product_img1=='') {
echo "<script> alert ('Please fill all the Fields') </script>";
exit();
}
$insert_product = "insert into products () value ()";
}
?>
<style>
body {background-color: grey;}
#form {
position:absolute;
left: 10%;
}
</style>
</body>
</html>
<?php
if(isset($_POST['insert_product'])){
//for text adding we can use following code
$product_title=$_POST ['product_title'];
$product_cat=$_POST ['product_cat'];
$product_brand=$_POST ['product_brand'];
$product_desc=$_POST ['product_desc'];
$product_price=$_POST ['product_price'];
$status='on';
$product_keywords=$_POST ['product_keywords'];
//for images we have to use following code
$product_img1= $_files ['product_img1'] ['name'];
$product_img2= $_files ['product_img2'] ['name'];
$product_img3=$_files ['product_img3'] ['name'];
//for images temporary names
$temp_img1= $_files ['product_img1'] ['temp_name'];
$temp_img2= $_files ['product_img1'] ['temp_name'];
$temp_img3= $_files ['product_img1'] ['temp_name'];
if($product_title=='' OR $product_cat=='' OR $product_brand=='' OR $product_desc=='' OR $product_price=='' OR $product_keywords=='' OR $product_img1=='') {
echo "<script> alert('Please fill all the Fields') </script>";
exit();
}
else {
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 (cat_id, brand_id, date, product_title, product_img1, product_img2, product_img3, product_desc, product_price, status) value (
'$product_cat','$product_brand','NOW()','$product_title','$product_img1','$product_img2','$product_img3','$product_desc','$product_price','$status')";
$run_product = mysqli_query($con, $insert_product);
if ($run_product){
echo "<script> alert('Product Added Successfuly')</script>";
}
}
}
?>
&#13;
答案 0 :(得分:2)
您的代码似乎很好。只需要做一些小的修复。
与 $ files 一样,您使用的应该是CAPS,如
$product_img1= $_FILES ['product_img1'] ['name'];
$product_img2= $_FILES ['product_img2'] ['name'];
$product_img3=$_FILES ['product_img3'] ['name'];
//for images temporary names
$temp_img1= $_FILES ['product_img1'] ['temp_name'];
$temp_img2= $_FILES ['product_img1'] ['temp_name'];
$temp_img3= $_FILES ['product_img1'] ['temp_name'];
我没有突出显示这一变化,因为其中一个答案已包含该部分。
毕竟,我不认为您的问题依赖于您的代码块。您是否检查了 PHP版本的设置。
当您没有使用 PHP的正确版本时,大多数问题通常会上升。
希望有助于此。 :)
答案 1 :(得分:0)
相关文章指出了你已经在代码中完成的事情,更确切地说是在使用变量进行任何操作之前使用isset()进行检查。但是当你处理$ _FILES超全局(http://php.net/manual/en/reserved.variables.files.php)时,我猜你忽略了一个关键方面:变量在PHP中总是区分大小写,与函数不同。
$product_img1= $_files ['product_img1'] ['name'];
$product_img2= $_files ['product_img2'] ['name'];
$product_img3=$_files ['product_img3'] ['name'];
//for images temporary names
$temp_img1= $_files ['product_img1'] ['temp_name'];
$temp_img2= $_files ['product_img1'] ['temp_name'];
$temp_img3= $_files ['product_img1'] ['temp_name'];
应该是:
$product_img1= $_FILES ['product_img1'] ['name'];
$product_img2= $_FILES ['product_img2'] ['name'];
$product_img3=$_FILES ['product_img3'] ['name'];
//for images temporary names
$temp_img1= $_FILES ['product_img1'] ['temp_name'];
$temp_img2= $_FILES ['product_img1'] ['temp_name'];
$temp_img3= $_FILES ['product_img1'] ['temp_name'];
实际上,我认为这些是未定义索引注意到的行。希望它有效。
答案 2 :(得分:0)
尝试将enctype =“multipart / form-date”更改为enctype =“multipart / form-data”