(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>