关于PHP和Bootstrap的本教程的未定义变量

时间:2015-06-13 16:49:50

标签: php html twitter-bootstrap

请帮我修复这个未定义的变量我将此代码复制并粘贴到我的create.php上,当我点击主页上的创建按钮时,我得到了这个:

  

注意:未定义的变量:fnameError in   第70行的C:\ xampp \ htdocs \ TestCRUD \ create.php

     

注意:未定义的变量:lnameError in   第75行的C:\ xampp \ htdocs \ TestCRUD \ create.php

     

注意:未定义的变量:ageError in   第80行的C:\ xampp \ htdocs \ TestCRUD \ create.php

注意:未定义的变量:第89行的C:\ xampp \ htdocs \ TestCRUD \ create.php中的genderError

<?php
if ( !empty($_POST)) {
    require 'db.php';
    // validation errors
    $fnameError     = null;
    $lnameError     = null;
    $ageError       = null;
    $genderError    = null;

    // post values
    $fname  = $_POST['fname'];
    $lname  = $_POST['lname'];
    $age    = $_POST['age'];
    $gender = $_POST['gender'];

    // validate input
    $valid = true;
    if(empty($fname)) {
        $fnameError = 'Please enter First Name';
        $valid = false;
    }

    if(empty($lname)) {
        $lnameError = 'Please enter Last Name';
        $valid = false;
    }

    if(empty($age)) {
        $ageError = 'Please enter Age';
        $valid = false;
    }

    if(empty($gender)) {
        $genderError = 'Please select Gender';
        $valid = false;
    }

    // insert data
    if ($valid) {
        $PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "INSERT INTO users (fname,lname,age,gender) values(?, ?, ?, ?)";
        $stmt = $PDO->prepare($sql);
        $stmt->execute(array($fname,$lname,$age,$gender));
        $PDO = null;
        header("Location: index.php");
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link   href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>

<body>
<div class="container">

                <div class="row">
                <div class="row">
                    <h3>Create a User</h3>
                </div>

                <form method="POST" action="">
<div class="form-group <?php echo !empty($fnameError)?'has-error':'';?>">
    <label for="inputFName">First Name</label>
    <input type="text" class="form-control" required="required" id="inputFName" value="<?php echo !empty($fname)?$fname:'';?>" name="fname" placeholder="First Name">
    <span class="help-block"><?php echo $fnameError;?></span>
</div>
<div class="form-group <?php echo !empty($lnameError)?'has-error':'';?>">
    <label for="inputLName">Last Name</label>
    <input type="text" class="form-control" required="required" id="inputLName" value="<?php echo !empty($lname)?$lname:'';?>" name="lname" placeholder="Last Name">
    <span class="help-block"><?php echo $lnameError;?></span>
</div>
<div class="form-group <?php echo !empty($ageError)?'has-error':'';?>">
    <label for="inputAge">Age</label>
    <input type="number" required="required" class="form-control" id="inputAge" value="<?php echo !empty($age)?$age:'';?>" name="age" placeholder="Age">
    <span class="help-block"><?php echo $ageError;?></span>
</div>
<div class="form-group <?php echo !empty($genderError)?'has-error':'';?>">
    <label for="inputGender">Gender</label>
    <select class="form-control" required="required" id="inputGender" name="gender" >
    <option></option>
    <option value="male" <?php echo $gender == 'male'?'selected':'';?>>Male</option>
    <option value="female" <?php echo $gender == 'female'?'selected':'';?>>Female</option>
    </select>
<span class="help-block"><?php echo $genderError;?></span>

</div>

<div class="form-actions">
    <button type="submit" class="btn btn-success">Create</button>
    <a class="btn btn-default" href="index.php">Back</a>
</div>
</form>

</div> <!-- /row -->
</div> <!-- /container -->
</body>
</html>

4 个答案:

答案 0 :(得分:0)

即使尚未设置变量,您也会显示错误。这会引起你看到的通知。您可以通过使用issetempty

检查是否先设置它们来避免它们
<?= isset($someError) ? $someError : '' ?>

答案 1 :(得分:0)

使用isset();

S3Exception in WrappedHttpHandler.php line 152:
Error executing "HeadObject" on "https://s3.Frankfurt.amazonaws.com/bucketName/resource-6";
AWS HTTP error: cURL error 6: Couldn't resolve host name

答案 2 :(得分:0)

在如下所示的行上,您需要在尝试使用它们之前检查变量是否具有值。所以下面的代码......

<span class="help-block"><? echo $fnameError; ?></span>

......应该改为:

<span class="help-block"><?= isset($fnameError) ? $fnameError : ''; ?></span>

为防止拾取和显示这些错误,您还可以关闭PHP错误报告,但最好解决问题:

<?php
error_reporting('off');
ini_set('display_errors', 'off');

答案 3 :(得分:0)

您可以在发送参数后为进程添加隐藏类型输入:

PHP:

<?php
if (isset($_POST['h'])) {
....//your process
}
?>

HTML:

<form action="" method="POST">
.....<!--your input-->
<input type="hidden" name="h" value="true">
<button type="submit"> submit </button>
</form>