[英]I am trying to validate jsp login form,I need field (label) validation instead of alert message
[英]I am trying to create this login/registration form with validation
我的代碼中出現某種php錯誤,但是我不確定這是什么,當您到達最底下的php時,我的代碼在結尾時開始變得有趣。 我正在嘗試驗證用戶名和密碼,但是卻遇到某種類型的錯誤
<?php
include 'config.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>Register Page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="main-wrapper">
<center>
<h2 style="color: white;">Register Page</h2>
<img class="avatar" src="spaceman2.jpg" style="height: 100px; width: 100px" />
</center>
<form class="my-form" action="register.php" method="post">
<label><b>Username<b></label>
<input name="username" type="text" class="input-values" placeholder="Your username" required/>
<label><b>Password<b></label>
<input name="password" type="password" class="input-values" placeholder="Your password" required/>
<label><b>Confirm Password<b></label>
<input name="cpassword" type="password" class="input-values" placeholder="Confirm password" required/>
<input name="signup_btn" id="signup-btn" type="submit" value="Sign Up"/>
<br/>
<a href="login.php"><input id="back-btn" type="button" value="<- Back"/></a>
</form>
<?php
if(isset($_POST['signup_btn']))
{
//echo '<script type="text/javascript"> alert("You are now signed in!")</script>';
$username = $_POST['username'];
$password = $_POST['password'];
$cpassword = $_POST['password'];
if($password == $cpassword)
{
$query= "SELECT * FROM user WHERE username ='$username'";
$query_run = mysqli_query($con,$query);
if(mysqli_num_rows($query_run)>0)
{
echo '<script type="text/javascript"> alert("Astronaut name already exist") </script>';
}
{
$query= "insert into user values('$username','$password')";
$query_run = mysqli_query($con, $query);
if($query_run)
{
echo '<script type="text/javascript"> alert("Astronaut is now registered! Go to Login Page!") </script>';
}
else
{
echo '<script type="text/javascript"> alert("Error!") </script>';
}
}
?>
一些東西:
您的$ cpassword引用了$ _POST ['password']而不是cpassword。
您的插入語句缺少VALUES之前的列引用。
您還應該考慮一些密碼哈希/加密。
您的代碼存在錯誤,原因是
特別是在PHP
您忘記了else
並使用了$cpasswrod
password
在html
您不會關閉</b>
<?php
include 'config.php';
if(isset($_POST['signup_btn']))
{
//echo '<script type="text/javascript"> alert("You are now signed in!")</script>';
$username = $_POST['username'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
if($password == $cpassword){
$query= "SELECT * FROM user WHERE username ='$username'";
$query_run = mysqli_query($con,$query);
if(mysqli_num_rows($query_run)>0)
{
echo '<script type="text/javascript"> alert("Astronaut name already exist") </script>';
}
else{
$query= "insert into user values('$username','$password')";
$query_run = mysqli_query($con, $query);
if($query_run)
{
echo '<script type="text/javascript"> alert("Astronaut is now registered! Go to Login Page!") </script>';
}
else
{
echo '<script type="text/javascript"> alert("Error!") </script>';
}
}
}
}
?>
<html>
<head>
<title>Register Page</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div id="main-wrapper">
<center>
<h2 style="color: white;">Register Page</h2>
<img class="avatar" src="spaceman2.jpg" style="height: 100px; width: 100px" />
</center>
<form class="my-form" action="index.php" method="post">
<label><b>Username</b></label>
<input name="username" type="text" class="input-values" placeholder="Your username" required/>
<label><b>Password</b></label>
<input name="password" type="password" class="input-values" placeholder="Your password" required/>
<label><b>Confirm Password</b></label>
<input name="cpassword" type="password" class="input-values" placeholder="Confirm password" required/>
<input name="signup_btn" id="signup-btn" type="submit" value="Sign Up"/>
<br/>
<a href="login.php"><input id="back-btn" type="button" value="<- Back"/></a>
</form>
</div>
</body>
</html>
希望對您有所幫助
這是您的代碼的新版本:(嘗試)
<?php
include 'config.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>Register Page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="main-wrapper">
<center>
<h2 style="color: white;">Register Page</h2>
<img class="avatar" src="spaceman2.jpg" style="height: 100px; width: 100px" />
</center>
<form class="my-form" action="register.php" method="post">
<label><b>Username</b></label>
<input name="username" type="text" class="input-values" placeholder="Your username" required />
<label><b>Password</b></label>
<input name="password" type="password" class="input-values" placeholder="Your password" required />
<label><b>Confirm Password</b></label>
<input name="cpassword" type="password" class="input-values" placeholder="Confirm password" required />
<input name="signup_btn" id="signup-btn" type="submit" value="Sign Up"/>
<br/>
<a href="login.php"><input id="back-btn" type="button" value="<- Back"/></a>
</form>
<?php
if(isset($_POST['signup_btn']))
{
//echo '<script type="text/javascript"> alert("You are now signed in!")</script>';
$username = $_POST['username'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
if($password == $cpassword)
{
$query= "SELECT * FROM user WHERE username ='$username'";
$query_run = mysqli_query($con,$query);
if(mysqli_num_rows($query_run)>0)
{
echo '<script type="text/javascript"> alert("Astronaut name already exist") </script>';
}else
{
$query= "insert into user (username, password) values('$username','$password')";
$query_run = mysqli_query($con, $query);
if($query_run)
{
echo '<script type="text/javascript"> alert("Astronaut is now registered! Go to Login Page!") </script>';
}
else
{
echo '<script type="text/javascript"> alert("Error!") </script>';
}
}
}
}
?>
</div>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.