0

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');

?>
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • What is the exact error message? – Barmar Nov 09 '20 at 23:45
  • 1
    Your code is wide open to SQL injection. See https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Barmar Nov 09 '20 at 23:45
  • In one location you are inserting into `products` but then you are querying from `categories` — are you sure that is correct? The root cause is likely trying to read `categoryName` when you seem to be adding `productName` (unless they are truly both meant to be different tables). – Pebbl Nov 10 '20 at 00:02
  • add your file productlist.php – Lukas Mittun Alexander Guldstv Nov 10 '20 at 02:15

0 Answers0