我在php中注册表单。我不知道如何验证我输入的电子邮件和(密码和确认密码)在PHP中。对于电子邮件,如果我输入相同的ID"它应该告诉我的电子邮件已经注册"并且对于密码和确认密码应检查是否相同。我在编码中尝试过电子邮件。它抛出错误电子邮件已经注册但我想在登录屏幕中显示错误。请帮助这两件事。我是非常新的PHP。抱歉我的英语不好。 我已在下面返回我的代码,
<?php
require('config.php');
// If the values are posted, insert them into the database.
if (isset($_POST['username']) && isset($_POST['password'])){
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$cpassword = $_POST['confirmpassword'];
$slquery = "SELECT 1 FROM register WHERE email = '$email'";
$selectresult = mysql_query($slquery);
if(mysql_num_rows($selectresult)>0)
{
die('email already exists');
}
$query = "INSERT INTO `register` (username, password,confirmpassword, email) VALUES ('$username', '$password', '$cpassword', '$email')";
$result = mysql_query($query);
if($result){
$msg = "User Created Successfully.";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login screen</title>
<style type="text/css">
.register-form{
width: 500px;
margin: 0 auto;
text-align: center;
padding: 10px;
padding: 10px;
background : #c4c4c4;
border-radius: 10px;
-webkit-border-radius:10px;
-moz-border-radius:10px;
}
.register-form form input{padding: 5px;}
.register-form .btn{
background: #726E6E;
padding: 7px;
border-radius: 5px;
text-decoration: none;
width: 50px;
display: inline-block;
color: #FFF;
}
.register-form .register{
border: 0;
width: 60px;
padding: 8px;
}
</style>
</head>
<body>
<div class="register-form">
<?php
if(isset($msg) & !empty($msg)){
echo $msg;
}
?>
<?php
if(isset($errormes))
{
echo $errormes;
}
?>
<h1>Register</h1>
<form action="" method="POST">
<p><label>User Name: </label>
<input type="text" id="username" name="username" placeholder="Username">
</p>
<p><label>E-Mail :</label>
<input id="email" name="email" type="email">
</p>
<p><label>Password :</label>
<input id="password" name="password" type="password">
</p>
<p><label>confirm Password :</label>
<input id="confirmpassword" name="confirmpassword" type="password">
</p>
<input type="submit" name="submit" value="register" class="btn register">
<a class="btn" href="login.php">Login</a>
</form>
</div>
</body>
</html>
答案 0 :(得分:2)
die()函数将有效地停止脚本的执行。这意味着没有更多的代码将在该行之后运行。
您可能希望使用某些内容作为标题()将用户重定向到显示不正确的登录屏幕的页面。
或者您可以包含该页面,就像您在示例中所做的那样。像这样:
<?php
require('config.php');
// If the values are posted, insert them into the database.
if (isset($_POST['username']) && isset($_POST['password'])){
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$cpassword = $_POST['confirmpassword'];
$slquery = "SELECT 1 FROM register WHERE email = '$email'";
$selectresult = mysql_query($slquery);
if(mysql_num_rows($selectresult)>0)
{
$msg = 'email already exists';
}
elseif($password != $cpassword){
$msg = "passwords doesn't match";
}
else{
$query = "INSERT INTO `register` (username, password,confirmpassword, email) VALUES ('$username', '$password', '$cpassword', '$email')";
$result = mysql_query($query);
if($result){
$msg = "User Created Successfully.";
}
}
}
?>
用这个替换你的第一个代码块。
答案 1 :(得分:0)
首先,欢迎使用PHP!
让我告诉你一件事, 以与执行源代码相同的方式编写源代码并不是一个好习惯。使用具有许多优点的功能。由于您是PHP的新手,并且您只是运行一个简单的要求,因此我有最适合您的解决方案。
现在请注意:
$msg
和$errormes
替换为echo
(它将打印出来
HTML内容之上的消息)。最好还是单独保存PHP和HTML(除非你真的需要它们,否则不要混在一起)。这是完整的代码。试试这个:
<?php
require_once('config.php');
// function for email validation
function is_valid_email($mail)
{
if (empty($mail)) {
echo "Email is required.";
return false;
} else {
$email = test_input($mail);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Invalid email format.";
return false;
}
// now check if the mail is already registered
$slquery = "SELECT 1 FROM register WHERE email = '$email'";
$selectresult = mysql_query($slquery);
if(mysql_num_rows($selectresult)>0) {
echo 'This email already exists.';
return false;
}
// now returns the true- means you can proceed with this mail
return true;
}
// function for password verification
function is_valid_passwords($pass,$confirmpass)
{
// Your validation code.
if (empty($pass)) {
echo "Password is required.";
return false;
}
else if ($pass != $confirmpass) {
// error matching passwords
echo 'Your passwords do not match. Please type carefully.';
return false;
}
// passwords match
return true;
}
// function for creating user
function create_user($username, $password, $cpassword, $email)
{
$query = "INSERT INTO `register` (username, password, confirmpassword, email) VALUES ('$username', '$password', '$cpassword', '$email')";
$result = mysql_query($query);
if($result){
return true; // Success
}else{
return false; // Error somewhere
}
}
// Code execution starts here when submit
if (isset($_POST['username']) && isset($_POST['password'])){
// Reading form values
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$cpassword = $_POST['confirmpassword'];
if (is_valid_email($email) && is_valid_passwords($password,$cpassword))
{
if (create_user($username, $password, $cpassword, $email)) {
echo 'New User Registered Successfully.';
}else{
echo 'Error Registering User!';
}
}
// You don't need to write another 'else' since this is the end of PHP code
?>
<!-- Here you go with the HTML -->
<!DOCTYPE html>
<html>
<head>
<title>Login screen</title>
<style type="text/css">
.register-form{
width: 500px;
margin: 0 auto;
text-align: center;
padding: 10px;
padding: 10px;
background : #c4c4c4;
border-radius: 10px;
-webkit-border-radius:10px;
-moz-border-radius:10px;
}
.register-form form input{padding: 5px;}
.register-form .btn{
background: #726E6E;
padding: 7px;
border-radius: 5px;
text-decoration: none;
width: 50px;
display: inline-block;
color: #FFF;
}
.register-form .register{
border: 0;
width: 60px;
padding: 8px;
}
</style>
</head>
<body>
<div class="register-form">
<h1>Register</h1>
<form action="" method="POST">
<p><label>User Name: </label>
<input type="text" id="username" name="username" placeholder="Username">
</p>
<p><label>E-Mail :</label>
<input id="email" name="email" type="email">
</p>
<p><label>Password :</label>
<input id="password" name="password" type="password">
</p>
<p><label>confirm Password :</label>
<input id="confirmpassword" name="confirmpassword" type="password">
</p>
<input type="submit" name="submit" value="register" class="btn register">
<a class="btn" href="login.php">Login</a>
</form>
</div>
</body>
</html>
现在以.php文件格式保存此代码并运行。你会看到你想要的方式。
在PHP中尝试实验。只是谷歌更多。只问 - 如果你被困了很长时间。
谢谢。