So I have three PHP files, the addProduct.php sends the form data to my add.php file add where it adds the data for that product and sends me back to my productList file.
The problem I am getting is that whenever I try to add the another data into my list I get an Notice: Undefined index error, however I believe there are no mistakes in my form in addProduct.php file and my add.php file that I see.
This is my addProduct.php
<?php
include('database.php');
$query='SELECT * FROM categories';
$products=$db->query($query);
?>
<header>
<h1 > Product manager</h1>
</header>
<main>
<h2>Add Product</h2>
<form action="add.php" method="post" id="addProduct">
<label>Category</label>
<select name="category_id">
<?php foreach($products as $product):?>
<option value="<?php echo $product['categoryID']?>"> <?php echo $product['categoryName']?></option>
<?php endforeach;?><br>
</select><br>
<label>Code</label>
<input type="text" name="code"><br>
<label>Name</label>
<input type="text" name="name"><br>
<label>Price</label>
<input type="text" name="price"><br>
<input type="submit" value="Add Product">
</form>
</main>
This is my add.php
<?php
include('database.php');
$category=$_POST['category_id'];
$code=$_POST['code'];
$name=$_POST['name'];
$price=$_POST['price'];
$query="INSERT INTO products
(categoryID, productCode, productName, listPrice) VALUES
('$category', '$code', '$name', '$price')";
$db->exec($query);
include('productList.php');
?>