我正在尝试将地址字段添加到我的注册表单中,但每当我提交寄存器时,我的地址行出错都是错误信息所说的
注意:未定义的索引:地址 第9行的C:\ xampp \ htdocs \ profile \ register \ index.php
这是php代码:
<?php
session_start();
if(isset($_REQUEST['sub']) && isset($_POST['gender'])){
$lname = strtoupper(trim(strip_tags($_POST['lname'])));
$fname = strtoupper(trim(strip_tags($_POST['fname'])));
$mname = strtoupper(trim(strip_tags($_POST['mname'])));
$cours = strtoupper(trim(strip_tags($_POST['gender'])));
$address =strtoupper(trim(strip_tags($_POST['address'])));
$email = strip_tags($_POST['email']);
$pass1 = strip_tags($_POST['pass']);
if(empty($lname) || empty($fname) || empty($mname) || empty($email) || empty($address)
|| empty($pass1) || empty($cours) || $_POST['gender'] == 'none' ){
$mess = "<font color='red'><i>Field(s) Required!</i></font>";
}
else if(!ctype_alpha($lname)||!ctype_alpha($fname)|| !ctype_alpha($mname)){
$mess = "<font color='red'><i>Incorrect Name Format!</i></font>";
}
else if(!preg_match('/^[a-zA-z0-9_.]*@[a-zA-z0-9.]*$/', $email)){
$mess = "<font color='red'><i>Incorrect Email Format!</i></font>";
}
else{
$host = "localhost";
$user = "root";
$pass = "";
$data = "school";
$connect = mysql_connect($host, $user, $pass);
mysql_select_db($data, $connect);
if(mysql_query("INSERT INTO studentinfo(`userid`, `lastname`, `firstname`, `middlename`, `email`, `address`, `password`, `gender`)
VALUES (null,'$lname','$fname','$mname','$email',`$address`,'$pass1','$cours') ")){
$mess = "Account Added to Database!";
}
else{
$mess = "<font color='red'><i>Data Already Exist!</i></font>";
}
}
}
if(isset($_REQUEST['back'])){
header( 'Location: ../' );
}
if(!empty($_SESSION['logon'])){
header( 'Location: ../' );
}
?>
答案 0 :(得分:1)
在该行之前检查是否定义了$_POST['address']
:
if(isset($_POST['address'])){
}
答案 1 :(得分:1)
您可能错过了名为“address”的输入字段。 您可以使用PHP检查:
<?php
// long way around
if(array_key_exists("address",$_POST){
// do something
}
else{
// not found
}
// short way
$address = array_key_exists("address",$_POST) ? $_POST["address"] : "";
?>
当然,您可以对所有字段执行此操作并添加您自己的代码。 否则,请告诉我们您的表单布局。你应该
<input type="text" name="address" />
代码。
答案 2 :(得分:1)
尝试这种方式:
if(isset($_REQUEST['sub']) && isset($_POST['gender']))
{
$address = isset($_POST['address']) ?
strtoupper(trim(strip_tags($_POST['address']))) : '';