我需要创建一个注册页面,用于存储用户名电子邮件密码并将其放入数据库中,以便用户可以登录并访问个人资料等。
我已经创建了一个数据库database但是没有任何内容可以进入它。我手动输入一个,但我尝试从网页上做的任何事情都不会进入数据库。
网页代码:注册是我想要显示的页面,而adduser是将数据添加到数据库的代码。
注册:
<?php include '../view/header.php';
?>
<br>
<br>
<h1 class="light white-text text-lighten-3">Sign up!</h1>
<br>
<br>
<form class="form" id="signup" action="addUser.php" method="post">
<div class="form-group ">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter Your Email">
</div>
<br>
<div class="form-group ">
<input id="user_name" type="text" class="validate" name="user_name"required="required">
<label for="user_name">User Name</label>
</div>
<br>
<div class="form-group col s6">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Enter a Password">
</div>
<br>
<br>
<button type="submit" class="orange btn btn-primary">Submit</button>
</form>
<?php
include '../view/footer.php';
ADDUSER:
<script src="../js/materialize.js" type="text/javascript"></script>
<script src="../js/materialize.min.js" type="text/javascript"></script>
<script src="../js/init.js" type="text/javascript"></script>
<?php
$server = "localhost";
$username = 'root';
$Password ="";
$database = 'commish';
$con = mysqli_connect($server, $username, $Password, $database);
$email = filter_input(INPUT_POST, 'email');
$user_name = filter_input(INPUT_POST, 'user_name');
$password = filter_input(INPUT_POST, 'password');
new_user( $user_name, $password,$email, $con);
function new_user($user_name, $password, $email,$con)
{
global $con;
$query = "INSERT into users (user_name, password, email) VALUES (:user_name, :password, :email)";
$statement = $con->prepare($query);
$statement->bindValue(":user_name", $user_name);
$statement->bindValue(":password", $password);
$statement->bindValue(":email", $email);
$statement->execute();
echo 'Successfully created new user';
}
答案 0 :(得分:1)
bindValue()
,mysqli
中没有PDO
方法。以下是解决问题的两种方法:
1)mysqli
方法:
使用bind_param()
方法将变量绑定到预准备语句。所以你的new_user()
函数应该是这样的:
function new_user($user_name, $password, $email,$con){
$query = "INSERT into users (user_name, password, email) VALUES (?, ?, ?)";
$statement = $con->prepare($query);
$statement->bind_param("sss", $user_name, $password, $email);
if($statement->execute()){
echo 'Successfully created new user';
}else{
// query failed
}
}
注意:由于您已将连接处理程序$con
传递给此功能,因此无需使用global $con;
。加Globals are evil。
2)PDO
方法:
保持new_user()
功能不变并更改此行
$con = mysqli_connect($server, $username, $Password, $database);
到
$con = new PDO("mysql:host=$server;dbname=$database",$username,$Password);
旁注:永远不要将密码存储为简单易读的文本,在将密码插入表格之前,请始终对原始密码执行salted password hashing。
答案 1 :(得分:0)
mysqli中没有bindValue()方法,你应该使用bind_param()
new_user function:
function new_user ($user_name, $password, $email)
{
global $con;
$stmt = $con->prepare("INSERT into users (user_name, password, email) VALUES (?,?,?)";
$stmt->bind_param("sss", $user_name, $password, $email);
$stmt->execute();
$stmt_error = $stmt->error;
$stmt->close();
if ($stmt_error)
echo 'Error on create new user: '.$stmt_error;
else
echo 'Successfully created a new user';
}