使用 HTML(Php) 表单向 Mysql 数据库添加数据时 PHP 出现未定义索引错误

Undefined Index error in PHP while adding data to Mysql database using a HTML(Php) Form

我已经阅读了您所有的相关文章,但没有找到我的解决方案。 实际上我正在创建一个电子商务网站,在我的项目中创建管理面板之后,我制作了一个 HTML(PHP) 表单以将数据添加到 Mysql(数据库)。每次都会出现以下错误。

"Notice: Undefined Index Error on a specific line in that Form"

我的代码在这里

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

}
}    
}   
   
?>

相关文章指向您已经在代码中完成的操作,更准确地说是在对变量执行任何操作之前使用 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'];

事实上,我认为这些是未定义索引通知指向的行。希望有用。

您的代码看起来不错。只需要做一些小的修复。

比如你用的$files应该大写,比如

$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.

版本不正确时,通常会出现大多数问题

希望对您有所帮助。 :)

尝试将 enctype="multipart/form-date" 更改为 enctype="multipart/form-data"