Mohiyo Deen Mohiyo Deen - 5 months ago 22
SQL Question

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

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

}
}
}

?>




Answer

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. :)

Comments