我尝试在我的代码中插入一个注册表单,但我总是打破我的模板。 我必须在哪里,但正确的代码? 我发现了一个注册脚本,我想把它推进去。 在输入标签中我添加了id标志,但每次我的模板
都会被破坏<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> 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();
}
}
?>
答案 0 :(得分:0)
这里有一些问题。您不应该混合数据库库(PDO
和mysql_
)。 mysql_*
需要完全删除。接下来,您应该在页面加载之前放置业务逻辑,而不是在中间。你应该有函数(类/方法会更好)隔离任务以便重用和灵活。这只是一个基本想法:
1)我建议您在站点的根目录中有一个config.php
文件,该文件具有常见的有用定义。
<强> /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)建立连接功能/类。查找连接的不同选项(UTF-8
,关闭prepare
仿真等)。
<强> /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)仅在您需要时加载功能来减少资源
function autoload($array)
{
foreach($array as $name) {
if(!function_exists($name))
require_once(FUNCTIONS.DS.$name.'.php');
}
}
<强> /functions/query.php 强>
4)使用自动绑定选项
创建查询功能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)只需进行通用返回即可简化操作。一旦你变得更高级,你就可以构建它
function fetch($query)
{
while($row = $query->fetch(PDO::FETCH_ASSOC)){
$result[] = $row;
}
return (!empty($result))? $result : false;
}
<强> /functions/addUser.php 强>
6)创建一个添加用户的功能,就是这样做
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)创建一个具有人类可读名称的检查用户功能。使您的if
/ else
条件更容易理解。
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 强>
使用包含和我们的自动加载功能将所有元素放在一起
<?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 -->