保存到数据库时如何将数据附加到用户输入?

时间:2014-05-06 07:32:11

标签: php jquery css email registration

我想创建一个像Gmail这样的注册表单。

我知道如何使用一些基本的PHP编码创建一个基本表单来验证并将其连接到MySQL数据库。但我希望拥有类似Gmail的用户名功能,用户可以注册电子邮件帐户而无需填写"@domain.com"

这是一张图片:https://flic.kr/p/nxxUqt

Screenshot

基本上,用户在注册时不必输入“@ gmail.com”,但“@ gmail.com”将插入MySQL数据库。

这是我的编码

<?php
    error_reporting(E_ALL ^ E_DEPRECATED ^ E_NOTICE ^ E_WARNING);
    $submit = $_POST['submit'];
    //form data
    $name = strip_tags($_POST['name']);
    $name2 = strip_tags($_POST['name2']);
    $username = strip_tags($_POST['username']);
    $email = strip_tags($_POST['email']);
    $password = strip_tags($_POST['password']);
    $password2 = strip_tags($_POST['password2']);
    $email2 = strip_tags($_POST['email2']);
    $address = strip_tags($_POST['address']);
    $address2 = strip_tags($_POST['address2']);
    $address3 = strip_tags($_POST['address3']);
    $address4 = strip_tags($_POST['address4']);
    $error = array();
    if ($submit) {
        //open database
        $connect = mysql_connect("localhost", "root", "") or die("Connection Error");
        //select database
        mysql_select_db("logindb") or die("Selection Error");
        //namecheck
        $namecheck = mysql_query("SELECT * FROM users WHERE username='{$username}' OR email='{$email}'");
        $count = mysql_num_rows($namecheck);
        if ($count!=0) {
            $error[] = "<div style='border:thin solid red'><span style='color:red'><b>ID or Display Name already taken</b></span></div>";
        }
        //check for existance
        if($name&&$name2&&$username&&$email&&$password&&$password2&&$email2&&$address&&$address2&&$address3&&$address4) {
            //encrypt password
            $password = md5 ($password);
            $password2 = md5 ($password2);
            if($_POST['password'] != $_POST['password2']) {
                $error[] = "<div style='border:thin solid red'><span style='color:red'>Password does not match</span></div>";
            }
            if(isset($error)&&!empty($error)) {
                implode($error);
            }
            else
            {
                //Registering to database
                $queryreg = mysql_query("INSERT INTO users VALUES ('','$name','$name2','$username','$email','$password','$password2','$email2','$address','$address2','$address3','$address4')");
                die ("<p>You have been registered! <br><a href='Login.php'>Click here to return to Login</a></p>");     
            }
        }
    }
    ?>

嗨大家好,感谢您现在帮忙我有验证问题,当我想验证我的用户名时,电子邮件验证与用户名一起出来 这是我的验证编码

    <?php
error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE ^ E_DEPRECATED);
$connect = mysql_connect("localhost", "root", "");
mysql_select_db("logindb");
$username = mysql_real_escape_string($_POST['username']);
$email = mysql_real_escape_string($_POST['email'].='@domain.com');
$password = mysql_real_escape_string($_POST['password']);
$password2 = mysql_real_escape_string($_POST['password2']);
$query = mysql_query("SELECT * FROM users WHERE username='{$username}' OR email='{$email}'");
$check = mysql_num_rows($query);
if($username==NULL) {
    echo "";
}
else
{
    if(strlen($username)<5) {
        echo "<span style='color:red'>Please enter 5 to 25 characters</span>";
    }
    else
    {
        if($check==0) {
        }
        else
        {
            if($check==1) {
                echo "<span style='color:red'>Display Name taken. Try another?</span>";
            }
        }
    }
}
if($email==NULL) {
    echo "";
}
else
{
    if(strlen($email)<5||strlen($email)>30) {
        echo "Please enter 5 to 30 characters";
    }
    else
    {
        if($check==0) {
        }
        else
        {
            if($check==1) {
            echo "Email Taken";
            }
        }
    }
}
if($password==NULL) {
    echo "";
}
else
{
    if(strlen($password)<6) {
        echo "<ul><li><span style='color:red'>Please enter at least 6 characters</span></li>";
    }
    if(!preg_match("#[A-Z]+#",$password)) {
        echo "<li><span style='color:red'>Please enter at least 1 upper case characters</span></li>";
    }
    if(!preg_match("#[0-9]+#",$password)) {
        echo "<li><span style='color:red'>Please enter at least 1 number</span></li>";
    }
    if(!preg_match("#[\W]+#",$password)) {
        echo "<li><span style='color:red'>Please enter at least 1 symbol</span></li></ul>";
    }
}
?>

1 个答案:

答案 0 :(得分:1)

我建议您将input控件封装在容器中(例如span)。在此跨度内,您还可以添加具有绝对定位的另一个元素(例如,anorther span)。

不要忘记将padding添加到input,这样当用户编写用户名时,用户名就不会与@domain内容重叠。

所以我的HTML是:

<span class="placeholder"><label>@domain</label><input type="text" name="username"/></span>

而我的CSS是:

 .placeholder {
     position:relative;
 }
 .placeholder label{
     position:absolute;
     right:0;
     margin-right:0.4em;
     color:#aaa;
 }
 .placeholder input{
     padding-right:5em;
 }

以下是一个工作示例:http://jsfiddle.net/Vjt6E/


然后在您的PHP代码中,您必须添加此值。

因此,如果您通过POST方法发送数据,则在PHP代码的开头添加:

<?php
    $_POST['username'].='@domain'

... rest of your code here ...

如果您使用GET请求发送,请执行以下操作:

<?php
    $_GET['username'].='@domain'

... rest of your code here ...