未定义的变量但已经声明

时间:2017-04-13 15:59:39

标签: php mysql

我试图制作一个简单的表单,可以检查姓名和电子邮件是否为空且格式不正确

这是我的代码

platform

我已经宣布了一个变量,但是当我在浏览器上查看它时,它会说:

<!DOCTYPE html> <html> <head> <title>Register</title> </head> <body> <?php include ('connect.php'); $first = $_POST['fname']; $last = $_POST['lname']; $email = $_POST['email']; if (isset($_POST['submit'])) { if ($first == '' || $first == ' ' || $last == '' || $last == ' '|| $email == "" || $email == " " || !preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last) || !filter_var($email, FILTER_VALIDATE_EMAIL)) { if ($first == '' || $first == ' ' ) { $fnameErr = "First name is required!"; } else { if(!preg_match("/^[a-zA-Z]*$/", $first)) { $fnameErr = "Only letters are allowed!"; } else { $fnameErr = ""; } } if($last == '' || $last == ' ') { $lnameErr = "Last name is required!"; } else { if(!preg_match("/^[a-zA-Z]*$/", $last)) { $lnameErr = "Only letters are allowed!"; } else { $lnameErr = ""; } } if ($email == "" || $email == " ") { $emailErr = "Email is required!"; } else { if(!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailErr = "Invalid email!"; } else { $emailErr = ""; } } } else { echo "<br>You've successfully created your account<br>"; mysql_query("INSERT into register SET FNAME='$first', LNAME='$last', EMAIL='$email'") or die(mysql_error()); } } ?> <h2 align="center">Registration Form</h2> <form action="" method="POST" align='center'> <p>First Name: <input type="text" name="fname">&nbsp;<?php echo $fnameErr; ?> <br> <p>Last Name: <input type="text" name="lname">&nbsp;<?php echo $lnameErr; ?> <br> <p>&nbsp;&nbsp; Email: &nbsp;&nbsp;&nbsp; <input type="text" name="email">&nbsp;<?php echo $emailErr; ?> <br><br> </p> <input type="submit" value="Submit" name="submit"> </form> </body> </html>

Notice: Undefined variable: fnameErr in /opt/lampp/htdocs/eam/add.php on line 81

Notice: Undefined variable: lnameErr in /opt/lampp/htdocs/eam/add.php on line 84

0 个答案:

没有答案