0

I have read all your Related Articles but didn't found my solution. Actually I was creating a E-Commerce site and after creating Admin-Panel in My Project when I made a HTML(PHP) Form to add data to Mysql(database). the following error comes everytime.

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

undefined index error in php

My code is here

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

}
}    
}   
   
?>

3 Answers 3

2

Your code seems fine. Just few minor fixes need to be done.

Like the $files you used should be in CAPS like

$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'];

Well I didn't highlight this change because one of the answers already contains that part.

After All, I don't think that your issue relies in your code blocks. Have you checked the settings of PHP Version.

Often most of the issues rise when you are not using the correct version of the PHP.

Hope That Helps. :)

Sign up to request clarification or add additional context in comments.

3 Comments

Yes I was using Xamp with Php 7.0 and many old functions were not working in New Xamp.
I still can't understand the reason of down vote I got. :(
Please, read how-to-answer for understanding why your answer is down voted. "Just few minor fixes need to be done" is not enough and requires more details. Could you add hint of improvements for this code?
0

The related articles point to something you've already done in your code, more exactly checking with isset() before doing anything with a variable. But I guess you overlooked a crucial aspect when you dealt with $_FILES superglobal (http://php.net/manual/en/reserved.variables.files.php): variables are always case-sensitive in PHP, unlike functions.

$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'];

Should be:

$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'];

In fact, I think these are the lines that the undefined index notices point to. Hope it works.

3 Comments

Let me check this issue and will return back to you
$_FILES also not working in my case. still the Error is same
Just had a look again, and saw you're using isset() only for $_POST['insert_product'] and that's obsolete now. Have you tried checking with isset() $_FILES ['product_img1'] ['name'] for instance? Try adding if (isset(...)) conditional before you try working with any superglobals, and see if it works. Give it a try.
0

try changing enctype="multipart/form-date" to enctype="multipart/form-data"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.