我已经在PHP中创建了一个用户注册表单,当我访问该页面时,表单的用户名部分已作为我自己的登录凭据填写,并且与密码相同。表单的其他部分也填充为未定义的索引。
下面显示了我的注册表单的表单和PHP代码。表单本身实际上可以工作并填充到我的数据库中。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">
<title>Ballymena Sports</title>
<!-- Bootstrap core CSS -->
<link href="bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="home2.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">Ballymena Sports</a>
</div>
<ul class="nav navbar-nav navbar-right">
<li><a href="logout.php">Log out</a></li>
</ul>
</div>
</nav>
<?php
include"config.php";
if(isset($_POST["submit"])){
$username=$_POST['username'];
$password=$_POST['password'];
$forename=$_POST['forename'];
$surname=$_POST['surname'];
$email=$_POST['email'];
$telephone=$_POST['telephone'];
$address1=$_POST['address1'];
$town=$_POST['town'];
$postcode=$_POST['postcode'];
$q = $db->prepare("SELECT * FROM user WHERE username = ?");
$query = $q-> execute(array($username));
$count = $q->rowCount();
if($count == 0) {
$query = $db->prepare("INSERT INTO user SET username = ?, password = ?, forename = ?, surname = ?, email = ?, telephone = ?, address1 = ?, town=?, postcode=? ");
$query = $query->execute(array($username,$password,$forename,$surname,$email,$telephone,$address1,$town,$postcode));
if($query){
echo "User successfully registered";
header("Location:home2_template.html");
return;
} else {
echo "Fail";
}
} else {
echo "User already exists";
}
}
?>
<!-- Main part of homepage -->
<div class="jumbotron">
<div class="container">
<div id="registerBody">
<h2>Register your account</h2>
<p>All fields within the registration form must be filled in</p>
</div>
<div class = "register">
<form method="POST" class="form-horizontal" action="">
<div class="form-group">
<label for="username" class="col-sm-2 control-label">Username:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="username" name="username" value="<?php echo $username ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" name="password" value="<?php echo $password ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="forename" class="col-sm-2 control-label">Forename:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="forename" name="forename" value="<?php echo $forename ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="surname" class="col-sm-2 control-label">Surname:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="surname" name="surname" value="<?php echo $surname ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email:</label>
<div class="col-sm-10">
<input type="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" class="form-control" id="email" name="email" placeholder="Match email format of 'email@provider.com'" value="<?php echo $email ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="telephone" class="col-sm-2 control-label">Telephone:</label>
<div class="col-sm-10">
<input type="text" pattern="[0-9]{11}" class="form-control" id="telephone" name="telephone" placeholder="Match telephone format of 11 digits" value="<?php echo $telephone ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="address1" class="col-sm-2 control-label">Address:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="address1" name="address1" value="<?php echo $address1 ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="town" class="col-sm-2 control-label">Town:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="town" name="town" value="<?php echo $town ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="postcode" class="col-sm-2 control-label">Postcode:</label>
<div class="col-sm-10">
<input type="text" pattern ="[A-Za-z]{1,2}[0-9Rr][0-9A-Za-z]? [0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}" class="form-control" id="postcode" name="postcode" placeholder="Match postcode format of 'XX00 0XX' "value="<?php echo $postcode ?>"required="required">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input id="button" name="submit" type="submit" value="Register" class="btn btn-primary">
</div>
</div> <!-- registration button -->
</form>
</div> <!-- registration end -->
</div> <!-- container end -->
</div> <!-- jumbo end -->
<br>
<!--<div class="form-group">
<div class="col-sm-10">
<input id="submit" name="reg" type="submit" value="Register" class="btn btn-primary">
</div>
</div> -->
<!-- end of reg -->
<div class="container">
<br>
<footer>
<p>© Ballymena Sports 2014</p>
</footer>
</div> <!-- /container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="../../dist/js/bootstrap.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>
&#13;
干杯 斯图尔特
答案 0 :(得分:5)
使用空字符串初始化所有变量。你的问题就是 解决。
<?php
$username="";
$password="";
$forename="";
$surname="";
$email="";
$telephone="";
$address1="";
$town="";
$postcode="";
if(isset($_POST["submit"])){
$username=$_POST['username'];
$password=$_POST['password'];
$forename=$_POST['forename'];
$surname=$_POST['surname'];
$email=$_POST['email'];
$telephone=$_POST['telephone'];
............
..........
......
..}