php mysql未定义索引的问题-新学习者需要说明

时间:2018-09-21 02:52:46

标签: php mysql

(我只是想知道为什么会这样)

我一直在这里的StackOverflow中学习PHP和MySQL,大多数时候,我都太兴奋了,无法将到目前为止所学到的东西结合在一起。尽管我尝试直接从自己的错误中了解事情,但仍有一些我无法自行解决。最近的问题是:

我已经研究了一个示例很长时间了,单击“添加新记录”按钮后,我不断收到以下错误消息。

尽管当我在页面上添加更多输入标签()时只有3个声明的变量时,我没有问题,也没有收到任何错误,但是我开始收到这些错误消息,并且插入查询无法通过。

下面是单击“添加新记录”按钮后显示的错误:

  

注意:未定义索引:C:\ xampp \ htdocs \ sppap \ create.php中的buyprice   在第54行

     

注意:未定义索引:C:\ xampp \ htdocs \ sppap \ create.php中的销售总额   在第76行

这是我的PHP和相关的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>

1 个答案:

答案 0 :(得分:0)

您从未关闭HTML元素上的class属性,因此name的设置不正确。看到此位:

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

您需要在class之前用"关闭该name。我不确定digitOnly是另一个类还是某些HTML5属性。

我猜你想要:

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

您还应该缩进代码,以便于阅读。

您的grosssale元素是disabled,因此不会发送。您可以在这里Disabled form inputs do not appear in the request中阅读更多内容。