将插入正确的位置

问题描述:

I try to insert a register form in my code but i broke always my template. Where i must but the code in the right way? i found a register script and i wanna push it inside. in the input tags i add the id flags but this destroyed every time my template

<div class="loginbox-or">
<div class="or-line"></div>
<div class="or">OR</div>
</div>
<div class="form-group">
<label>Email: <span class="required">*</span></label>
<input placeholder="" class="form-control" type="email">
</div>
<div class="form-group">
<label>Password: <span class="required">*</span></label>
<input placeholder="" class="form-control" type="password">
</div>
<div class="form-group">
<label>Confirm Password: <span class="required">*</span></label>
<input placeholder="" class="form-control" type="password">
</div>
<div class="loginbox-forgot">
<input type="checkbox"> I accept <a href="">Term and consitions?</a>
</div>
<div class="loginbox-submit">
<input type="button" class="btn btn-default btn-block" value="Register">
</div>
<div class="loginbox-signup"> Already have account <a href="login.html">Sign in</a> </div>
</div>
</div>
</div>
</div>
</div>
</section>



<script type="text/javascript">
$(".full-page").height($(window).height());
$(window).resize(function() {
$(".full-page").height($(window).height());
});
</script>

</div>
</body>

here i downloaded my register script.

<div class="signin-form">

<div class="container">


<form class="form-signin" method="post" id="register-form">

<h2 class="form-signin-heading">Sign Up</h2><hr />

<div id="error">
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Email address" name="user_email" id="user_email" />
<span id="check-e"></span>
</div>

<div class="form-group">
<input type="password" class="form-control" placeholder="Password" name="password" id="password" />
</div>

<div class="form-group">
<input type="password" class="form-control" placeholder="Retype Password" name="cpassword" id="cpassword" />
</div>
<hr />

<div class="form-group">
<button type="submit" class="btn btn-default" name="btn-save" id="btn-submit">
<span class="glyphicon glyphicon-log-in"></span> &nbsp; Create Account
</button>

PHP-PART

<?php

        require_once 'dbconfig.php';

        if($_POST)
        {
        $user_email = mysql_real_escape_string($_POST['user_email']);
        $user_password = mysql_real_escape_string($_POST['password']);
        $joining_date = date('Y-m-d H:i:s');

        //password_hash see : http://www.php.net/manual/en/function.password-hash.php
        $password = password_hash( $user_password, PASSWORD_BCRYPT, array('cost' => 11));

        try
        {
        $stmt = $db_con->prepare("SELECT * FROM tbl_users WHERE user_email=:email");
        $stmt->execute(array(":email"=>$user_email));
        $count = $stmt->rowCount();

        if($count==0){
        $stmt = $db_con->prepare("INSERT INTO tbl_users(user_email,user_password,joining_date) VALUES(:email, :pass, :jdate)");
        $stmt->bindParam(":email",$user_email);
        $stmt->bindParam(":pass",$password);
        $stmt->bindParam(":jdate",$joining_date);

        if($stmt->execute())
        {
        echo "registered";
        }
        else
        {
        echo "Query could not execute !";
        }

        }
        else{

        echo "1"; // not available
        }

        }
        catch(PDOException $e){
        echo $e->getMessage();
        }
        }

        ?>

You have a few issues going on here. You should not be mixing database libraries (PDO and mysql_). mysql_* needs to just be removed entirely. Next, you should put the business logic before your page loads, not in the middle. You should have functions (class/methods would be better) that isolate tasks for reuse and flexibility. Here is just a basic idea:

1) I suggest you have a config.php file located in the root of you site that has common helpful defines.

/config.php

define('DS',DIRECTORY_SEPARATOR);
define('ROOT_DIR',__DIR_);
define('FUNCTIONS',ROOT_DIR.DS.'functions');
define('DB_HOST','localhost');
define('DB_NAME','databasename');
define('DB_USERNAME','root');
define('DB_PASSWORD','password');

2) Make a connection function/class. Look up the different options for the connection (UTF-8, turn off prepare emulation, etc).

/functions/connect.php

function connect()
    {
        try {
            $con = new \PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME,DB_USERNAME,DB_PASSWORD);
            return $con;
        }
        catch(PDOException $e) {
            die('An error has occurred.');
        }
    }

/functions/autoload.php

3) Cut down on resources by loading functions only when you need them

function autoload($array)
    {
        foreach($array as $name) {
            if(!function_exists($name))
                require_once(FUNCTIONS.DS.$name.'.php');
        }
    }

/functions/query.php

4) Create a query function with auto-bind option

function query($con,$sql,$bind=false)
    {
        if(!empty($bind)) {
            foreach($bind as $key=>$value) {
                $bKey = ":{$ey}";
                $bindArr[$bKey] = $value;
            }

            $query = $con->prepare($sql);
            $query->execute($bindArr);

            return $query;
        }
        else {
            return $con->query($sql);
        }
    }

/functions/fetch.php

5) Just do a generic return to simplify things. Once you get more advanced, you can build this out

function fetch($query)
    {
        while($row = $query->fetch(PDO::FETCH_ASSOC)){
            $result[] = $row;
        }

        return (!empty($result))? $result : false;
    }

/functions/addUser.php

6) Make a function that adds a user, and that's all it does

function addUser($con,$username,$password)
    {
        $joined = date('Y-m-d H:i:s');
        $password = password_hash($password, PASSWORD_BCRYPT, array('cost' => 11));

        try{
            query("INSERT INTO tbl_users(user_email,user_password,joining_date) VALUES(:0, :1, :2)",array($username,$password,$joined));
            return true;
        }
        catch(PDOException $e) {
            return false;
        }
    }

/functions/userExists.php

7) Create a check user function with a human-readable name. Makes your if/else conditionals easier to follow.

function userExists($con,$username)
    {
        $countUser = query($con,"SELECT COUNT(*) as count FROM tbl_users WHERE user_email=:0",array($username));
        $count = fetch($countUser);
        return ($count[0]['count'] > 0);
    }

/signup.php

Put all the elements together using includes and our autoload function

<?php
# Add our config file
require_once(__DIR__.DIRECTORY_SEPARATOR.'config.php');
# Add our autoload file
require_once(FUNCTIONS.DS.'autoload.php');
# Use our autoload to define our functions we will be using
autoload(array('connect','query','fetch','userExists','addUser'));
# Create your connection
$con     = connect();
# Check for sign up
if(!empty($_POST['signup'])) {
    # Since our names are human-readable, you can easily follow what is happening...
    if(!userExists($con,$_POST['user_email'])){
        if(!addUser($con,$_POST['user_email'],$_POST['password'])){
            $error = 'Error occurred.';
        }
        else {
            $success = true;
        }
    }
    else {
        $error = 'User exists';
    }
}
?><!DOCTYPE html>
....etc.
<!-- Down in the body, you can echo the error or success -->