始终返回表单中的错误

时间:2011-03-19 23:19:41

标签: php forms

我有这个注册表格,但无论出于什么原因,它总是返回用户名。有人关心它吗?

<?php
session_start;
include "config.php";

if($_POST['register']) {
if($_POST['email']) {
if($_POST['name']) {
if($_POST['pw1']) {
if($_POST['pw2']) {
if($_POST['pw2'] == $_POST['pw1']) {

$check_query = mysql_query("SELECT * FROM register WHERE username = '" .$_POST['name']. "'");
$check = mysql_num_rows($check_query);
if($check == "0"){
$pw = md5($_POST['pw1']);

mysql_query("INSERT INTO register (username, pass, email)  
    VALUES ('".$_POST['name']."', '".$pw."', '".$_POST['email']."')") or die(mysql_error());
    $notice = "You are registered and you can login <a href='index.php'>here</a>";
}else{//#1 name already exists
$notice = "This username already exists";
}//#1 close
}else{//#2 pw's not the same
$notice = "The passwords are not the same";
}//#2 close
}else{//#3 pw2 not filled in
$notice = "You do have to type in a password";
}//#3 close
}else{//#4 pw 1 not filled in
$notice = "You do have to type in a password";
}//#4 close
}else{//#5 no name
$notice = "You do have to type in a username";
}//#5 close
}else{//#6 no email
$notice = "You do have to type in an email";
}//#6 close
}//post register close
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="bluestyle.css">
<link href='http://fonts.googleapis.com/css?family=Cabin' rel='stylesheet' type='text/css'>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sign Up</title>
</head>

<body>
<center><div id="masterpower">
<img src="headblue.png" id="head">
<div id="h">
                <a style="float:right;margin-right:10px;">This</a>
            </div>
            <div class="holder">

            <div class="loppit">
                    <div id="contt">
                            <h1>Sign up!</h1><br />
<?php
if($_SESSION['usrid']) {
echo 'You are already logged in, <br /><a href="index.php">Home</a>';
}else{
if($notice) {
echo $notice, '<br /><a href="javascript:history.go(-1)">Back</a>';
}else{
?>
Note: Everything is case-sensitive!
<form id="form1" name="form1" method="post" class="form" action="register.php">
<table>
  <tr><td>Username:</td>
  <td><input name="name" id="name" class="ipted" type="text" /></td></tr>
  <tr><td>Password:</td>
  <td><input type="password" class="ipted" id="pw2" name="pw2"/></td></tr>
  <tr><td>Password (Verify):</td>
  <td><input name="pw1" class="ipted" id="pw1" type="password" /></td></tr>
  <td>E-Mail:</td>
  <td><input type="text" class="ipted" id="email" name="email" /></td></tr>
  <tr><td></td><td><input type="submit" class="ipted" name="register" value="Register" id="register" /></td></tr>
</form>
<?php
}
}
?>
</div>
                    </div>
            </div>

</div>
    </center>
</body>
</html>

1 个答案:

答案 0 :(得分:4)

这有点整洁:

<?php
session_start();

include "config.php";

if($_POST['register']) {

    $notice = array();

    if(empty($_POST['email'])) {
        $notice[] = "You do have to type in an email";
    }

    if(empty($_POST['name'])) {
        $notice[] = "You do have to type in a username";
    }

    if(empty($_POST['pw1']) || empty($_POST['pw2'])) {
        $notice[] = "You do have to type in a password";
    }

    if($_POST['pw2'] == $_POST['pw1']) {
        $notice[] = "The passwords are not the same";
    }

    $check_query = mysql_query("SELECT * FROM register WHERE username = '" .$_POST['name']. "'") or die(mysql_error()); // added " or die(mysql_error())" for debug purpose

    if(mysql_num_rows($check_query) != 0){
        $notice[] = "This username already exists"; 
    }

    if(count($notice) == 0){
        $pw = md5($_POST['pw1']);
        mysql_query("INSERT INTO register (username, pass, email)  
            VALUES ('".$_POST['name']."', '".$pw."', '".$_POST['email']."')") or die(mysql_error());
            $notice[] = "You are registered and you can login <a href='index.php'>here</a>";
    }

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="bluestyle.css">
<link href='http://fonts.googleapis.com/css?family=Cabin' rel='stylesheet' type='text/css'>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sign Up</title>
</head>

<body>
<center><div id="masterpower">
<img src="headblue.png" id="head">
<div id="h">
                <a style="float:right;margin-right:10px;">This</a>
            </div>
            <div class="holder">

            <div class="loppit">
                    <div id="contt">
                            <h1>Sign up!</h1><br />
<?php
if($_SESSION['usrid']) {
    echo 'You are already logged in, <br /><a href="index.php">Home</a>';
}else{
    if(isset($notice) && count($notice) > 0) {
        echo implode(', ',$notice), '<br /><a href="javascript:history.go(-1)">Back</a>';
    }else{
        ?>
        Note: Everything is case-sensitive!
        <form id="form1" name="form1" method="post" class="form" action="register.php">
        <table>
          <tr><td>Username:</td>
          <td><input name="name" id="name" class="ipted" type="text" /></td></tr>
          <tr><td>Password:</td>
          <td><input type="password" class="ipted" id="pw2" name="pw2"/></td></tr>
          <tr><td>Password (Verify):</td>
          <td><input name="pw1" class="ipted" id="pw1" type="password" /></td></tr>
          <td>E-Mail:</td>
          <td><input type="text" class="ipted" id="email" name="email" /></td></tr>
          <tr><td></td><td><input type="submit" class="ipted" name="register" value="Register" id="register" /></td></tr>
        </form>
        <?php
    }
}
?>
</div>
                    </div>
            </div>

</div>
    </center>
</body>
</html>

试一试,看看是否收到错误。