-3

以下代码能够注册用户,但无法检查用户是否已存在。请修改代码以检查用户是否已存在。我正在使用从 navision 表中获取数据的 sql server 2012。该代码包含两个部分,即 php 代码和 html 代码(其中包含注册表单)

<?php 
include 'core/database/conn.php';
include 'includes/overall/header.php';
// Process the POST data.
if (isset($_POST['log22'])){
$username = $_POST['username'];
$password = $_POST['password'];
$confirm_password = $_POST['confirm'];
$email = $_POST['email'];
//echo $getemail;
//echo "<script>alert('The Email is already registered');</script>";

        $id=md5(rand(1,123));
        $id='ADM'.strtoupper(substr($id,0,10));

        $username = strtoupper($username);
        $password = substr(md5($password),0,9);
        $email = strtoupper($email);


        //header ('Location: register.php');

        $sql = "INSERT INTO[ICPAK_FORMS].[dbo].[ICPAK\$User Register](
                                                        [ID],[Username],[Password],[Email])                         
                                                VALUES  ('$id','$username','$password','$email')"; 

        global $DBCONN; 
        odbc_exec($DBCONN,$sql);

}
?>
<h1>Register</h1>

    <!----><script type="text/javascript" src="js/regform.js"></script>
    <form id="formreg" name="formreg" method="post" action="register.php" onSubmit="return regformValidation()">
        <ul>

            <li>
        Username:<br/><input type="text"  name="username" value="<?php //if(isset($_POST['username'])){ echo $_POST['username']; } ?>"/>
         </li>
            <li>
        Password:<br/> <input minlength="5" type="password"  name="password" value="<?php //if(isset($_POST['password'])){ echo $_POST['password']; } ?>"/>
         </li>
            <li>
        Confirm Password:<br/><input type="password"  name="confirm"  value="<?php //if(isset($_POST['confirm'])){ echo $_POST['confirm']; } ?>" />
         </li>
            <li>
        E-mail<br/>
        <input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])){ echo $_POST['email']; } ?>"/>   
        </li>
            <li>
        <input type="submit" value="submit" /><input type="hidden" name="log22" id="log22" value="Login" /></td>
    </li>
            <li>

    <table>
        <tr><td colspan="2">If you are registered click <a href="login.php">here</a> to Login</td><tr>
        </table>
        </li>           
        </ul>
    </form>
<?php include 'includes/overall/footer.php';?>
4

2 回答 2

1

您可以简单地做的是检查输入用户名的用户是否存在,您可以按照以下方式进行操作.. 例如

$result = odbc_exec( $connection, "SELECT username FROM users_table WHERE username like '$username' ");
if ( odbc_num_rows($queryresult) > 0) {
    echo 'User with this name already exist.. Please select a different username';
    ....
    ....
    // your logic what you want to do after that
}
else{
       //Save and Register User
}

我建议你自己努力去做,如果你努力后失败了,那么你应该在这里问..反正祝你好运

于 2012-10-09T07:23:36.733 回答
0

写这个

$sql_select = "select * from [ICPAK_FORMS].[dbo].[ICPAK\$User Register] where [Username] = $username";
$queryresult = odbc_exec($DBCONN,$sql_select);
if ( odbc_num_rows($queryresult) > 0 ) { 
{
     // code for insert
}
else {
   // show error that username already exist.
}
于 2012-10-09T07:21:37.580 回答