0

(I just want a specific answer as to why this happens)

I have been learning PHP and MySQL here in StackOverflow and most of the time I get too excited and combine things that I have Learned from here so far. Though I try to understand things directly from the mistakes I make, there are still some things I am not able to solve on my own. The most recent problem is:

I have been working on an example for quite a while and I am constantly getting the below error message after clicking the "Add New Record" button.

Though I have no problems and have not received any errors when I only have 3 declared variables when I added more input tags () to my page, I start to receive these error messages and my insert query couldn't get through.

Below are the errors that show after clicking the "Add New Record" button:

Notice: Undefined index: buyprice in C:\xampp\htdocs\sppap\create.php on line 54

Notice: Undefined index: grosssale in C:\xampp\htdocs\sppap\create.php on line 76

and here is my PHP and relevant HTML:

PHP

<?php
    require_once 'config.php';
    $brand = $generic = $wgtvol = $unit =  $manufacturer = "";
    $buyprice = $sellprice = $grosssale = "";

    $brand_err = $generic_err = $wgtvol_err = $unit_err = $manufacturer_err = $buyprice_err = $sellprice_err = $grosssale_err = ""; 

    if($_SERVER["REQUEST_METHOD"] == "POST"){
        $input_brand = trim($_POST["brand"]);
        if(empty($input_brand)){
            $brand_err = '<b style="color: #960303; font-size: 160%">Please enter the brand name of the product.</b>';
        } else{
            $brand = $input_brand;
        }
        $input_generic = trim($_POST["generic"]);
        if(empty($input_generic)){
            $generic_err = '<b style="color: #960303; font-size: 160%">Please enter the generic name of the product.</b>';
        } else{
            $generic = $input_generic;
        }
        $input_wgtvol = trim($_POST["wgtvol"]);
        if(empty($input_wgtvol)){
            $wgtvol_err = '<b style="color: #960303; font-size: 160%">Please enter the weight or volume of the product.</b>';
        } else{
            $wgtvol = $input_wgtvol;
        }

        $input_manufacturer = trim($_POST["manufacturer"]);
        if(empty($input_manufacturer)){
            $manufacturer_err = '<b style="color: #960303; font-size: 160%">Please enter the manufacturer of the product.</b>';
        } else{
            $manufacturer = $input_manufacturer;
        }
        $input_unit = trim($_POST["unit"]);
        if(empty($input_unit)){
            $unit_err = '<b style="color: #960303; font-size: 160%">Please choose a measurement unit of the product.</b>';
        } else{
            $unit  = $input_unit ;
        }
        $input_buyprice = trim($_POST["buyprice"]);
        if(empty($input_buyprice)){
            $buyprice_err = '<b style="color: #960303; font-size: 160%">Please enter the buy price of the product.</b>';
        } else{
            $buyprice = $input_buyprice;
        }
        $input_sellprice = trim($_POST["sellprice"]);
        if(empty($input_sellprice)){
            $sellprice_err = '<b style="color: #960303; font-size: 160%">Please enter the sell price of the product.</b>';
        } else{
            $sellprice = $input_sellprice;
        }
        $input_grosssale = trim($_POST["grosssale"]);
        if(empty($input_grosssale)){
            $grosssale_err = '<b style="color: #960303; font-size: 160%">This field is suppose to auto-generate after typing the buy price and the sell price.<br>There is no need to manually enter an amount into this field.</b>';
        } else{
            $grosssale = $input_grosssale;
        }
        $generic = $brand = $wgtvol = $unit = $manufacturer = $buyprice = $sellprice = $grosssale = "";
        if(empty($generic_err) && empty($brand_err) && empty($wgtvol_err) && empty($unit_err) && empty($manufacturer_err) && empty($buyprice_err) && empty($sellprice_err) && empty($grosssale_err)){
            $sql = "INSERT INTO productslist (generic, brand, wgtvol, unit, manufacturer, buyprice, sellprice, grosssale) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
            if($stmt = mysqli_prepare($link, $sql)){
                mysqli_stmt_bind_param($stmt, "ssssssss", $param_generic, $param_brand, $param_wgtvol, $param_unit, $param_manufacturer, $param_buyprice, $param_sellprice, $param_grosssale);
                $param_generic = $input_generic;
                $param_brand = $input_brand;
                $param_wgtvol = $input_wgtvol;
                $param_unit = $input_unit;
                $param_manufacturer = $input_manufacturer;
                $param_buyprice = $input_buyprice;
                $param_sellprice = $input_sellprice;
                $param_grosssale = $input_grosssale;
                if(mysqli_stmt_execute($stmt)){
                    header("location: index.php");
                    exit();
                } else{
                    echo "Something went wrong. Please try again later.";
                }
            }
            mysqli_stmt_close($stmt);
        }
        mysqli_close($link);
    }
?>

HTML

<form class="form-wrapper" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
    <div class="form-group <?php echo (!empty($brand_err)) ? 'has-error' : ''; ?>">
        <label>Brand Name</label>
        <input type="text" name="brand" class="form-control" value="<?php echo $brand; ?>" />
        <span class="help-block"><?php echo $brand_err;?></span>
    </div>
    <div class="form-group <?php echo (!empty($generic_err)) ? 'has-error' : ''; ?>">
        <label>Generic Name</label>
        <input type="text" name="generic" class="form-control" value="<?php echo $generic; ?>" />
        <span class="help-block"><?php echo $generic_err;?></span>
    </div>
    <div class="form-group <?php echo (!empty($wgtvol_err)) ? 'has-error' : ''; ?>">
        <div class="input-group">
            <label class="black-text">Weight / Volume</label>
            <input type="number" step="0.01" maxlength="7" class="form-control digitOnly pull-left" id="wgtvol" name="wgtvol" value="<?php echo $wgtvol; ?>" />
            <label class="sr-only black-text">Unit</label>
            <select  name="unit" class="form-control pull-left" id="unit" type="text"  >
                <option disabled value="">select</option>
                <option value="mg"> mg </option>
                <option value="gram(s)"> gram(s) </option>
                <option value="ml"> ml </option>
                <option value="liter(s)">liter(s)</option>
                <option value="dl"> dl </option>
                <option value="cc"> cc </option>
                <option value="pc(s)"> pc(s) </option>
                <option value="fl oz">fl oz</option>
                <option value="gal">gal</option>
            </select>
        </div>
    </div>
    <div class="form-group <?php echo (!empty($manufacturer_err)) ? 'has-error' : ''; ?>">
        <label>Manufacturer</label>
        <input type="text" name="manufacturer" class="form-control" value="<?php echo $manufacturer; ?>" />
        <span class="help-block"><?php echo $manufacturer_err;?></span>
    </div>
    <div class="form-group <?php echo (!empty($buyprice_err)) ? 'has-error' : ''; ?>">
        <label>Buy Price</label>
        <input type="number" step="0.01" maxlength="7" class="form-control digitOnly name="buyprice" class="form-control" value="<?php echo $buyprice; ?>"  />
        <span class="help-block"><?php echo $buyprice_err;?></span>
    </div>=
    <div class="form-group <?php echo (!empty($sellprice_err)) ? 'has-error' : ''; ?>">
        <label>Sell Price</label>
        <input type="number" step="0.01" maxlength="7" class="form-control digitOnly" name="sellprice" value="<?php echo $sellprice; ?>"  />
        <span class="help-block"><?php echo $sellprice_err;?></span>
    </div>
    <div class="form-group <?php echo (!empty($grosssale_err)) ? 'has-error' : ''; ?>">
        <label>Gross Sale</label>
        <input disabled type="number" maxlength="7" class="form-control digitOnly" name="grosssale" value="<?php echo $grosssale; ?> "  />
        <span class="help-block"><?php echo $grosssale_err;?></span>
    </div>
    <input type="submit" class="btn btn-primary" value="Add New Record" />
    <a href="index.php" class="btn btn-default">Cancel</a>
</form>
Timothy Bomer
  • 504
  • 5
  • 21
makLoy
  • 23
  • 5
  • 2
    Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Obsidian Age Sep 21 '18 at 02:54

1 Answers1

0

You never closed the class attribute on your HTML element so the name isn't set correctly. See this bit:

class="form-control digitOnly name="buyprice"

You need to close that class with a " before the name. I'm not sure if digitOnly is another class or some HTML5 attribute.

I'd guess you wanted:

class="form-control digitOnly" name="buyprice"

You also should indent your code so it is easier to read.

Your grosssale element is disabled so it will not send. You can read more here, Disabled form inputs do not appear in the request.

user3783243
  • 5,368
  • 5
  • 22
  • 41
  • no, digitOnly is another separate cLass.. i am just wondering if i have to repLace my decLaration of the variabLes that has error notifications. and my with the grosssale is intendedLy disabLed as it wiLL automaticaLLy popuLate upon enterring the buy and the seLL price.. – makLoy Sep 21 '18 at 03:33
  • The error is because the super global `$_POST` doesn't have an index with those 2 names. The first will be corrected by changing the quotes. You'll need to sort out how your JS works. As is ` – user3783243 Sep 21 '18 at 03:39
  • so how do i do this correctLy? do i have to change my decLaration script? Like using $_GET or $_SESSION or anything eLse? or do i have to use singLe quotes instead of doubLe quotes? – makLoy Sep 21 '18 at 03:46
  • No, you are sending over `POST`, `method="post"`. Make the changes in my answer and it should work, or at least fix the first. For the second you'll need to fix your JS, or see why that value isn't being sent. Use the debugging method I posted in my last comment. – user3783243 Sep 21 '18 at 03:49
  • by the way, the reason why i write codes that way is that due to my visuaL disabiLity, i heaviLy reLy on screenreader and it is very much of a work for me to add additionaL spaces just to check each character individuaLLy.. – makLoy Sep 21 '18 at 04:00
  • or is there instead a better way to decLare my variabLes instead? i think i am using an oLder styLe of decLaring my variabLes.. $brand = $generic = $wgtvol = $unit = $manufacturer = $buyprice = $sellprice = $grosssale = ''; – makLoy Sep 21 '18 at 04:13
  • or these type of decLaration even more acceptabLe? $brandname = $brandname ?? ''; $genericname = $genericname ?? ''; $wgtvol = $wgtvol ?? ''; etc. – makLoy Sep 21 '18 at 04:34
  • @makLoy, there is nothing wrong in the way you do the default assignment of your variables. The null coalescing operator doesn't help there. – Progrock Sep 21 '18 at 04:45
  • @makLoy, after fixing your html form input elements, perhaps post your code on codereview: https://codereview.stackexchange.com – Progrock Sep 21 '18 at 04:48