使用MySql和PHP对html页面进行验证和数据库连接
这是我的代码,用于创建一个简单的登录页面,其中包含用户名、密码、确认密码和自我识别使用MySql和PHP对html页面进行验证和数据库连接,php,html,mysql,Php,Html,Mysql,这是我的代码,用于创建一个简单的登录页面,其中包含用户名、密码、确认密码和自我识别select选项:- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <
select
选项:-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Sign up</title>
<style type="text/css">
div#registration
{
text-align:center;
margin-left:auto;
margin-right:auto;
width:40%;
border:solid 2px green;
}
</style>
</head>
<body>
<h1> Sign Up! </h1>
<h2> Enter username and password. Dont forget to recognize yourself </h2>
<?php
$mysqli = new mysqli("localhost", "root", "", "signup");
if($mysqli===false)
{
die("ERROR: Could not connect to database." .mysqli_connect_error());
}
if(isset($_POST['submit']))
{
echo'<div id="message">';
$inputError = false;
if(empty($_POST['username'] ))
{
echo 'ERROR: Please enter a valid user name';
$inputError = true;
}
else
{
$username= $mysqli->escape_string($_POST['username']);
}
if($inputError != true && empty($_POST['password'] ))
{
echo 'ERROR: Please enter valid password';
$inputError = true;
}
else
{
$password= $mysqli->escape_string($_POST['password']);
}
$inputError = false;
if(empty($_POST['cfmpassword'] ))
{
echo 'ERROR: empty field';
$inputError = true;
}
else if($_POST['password'] != $_POST['cfmpassword'])
{
echo 'ERROR: passwords does not match';
}
else
{
$cfmpassword = $mysqli->escape_string($_POST['cfmpassword']);
}
if($inputError != true && empty($_POST['desig'] ))
{
echo 'ERROR: Please enter valid password ';
$inputError = true;
}
else
{
$desig= $mysqli->escape_string($_POST['desig']);
}
if ($inputError !=true)
{
$sql= "INSERT INTO database (Username, password, confirm password, Designation) VALUES ('$username','$password','$cfmpassword','$desig')";
if ($mysqli->query($sql) === true)
{
echo 'new record added with ID:' . $mysqli->insert_id;
}
else
{
echo "ERROR: Could not execute query: $sql." .$mysqli->error;
}
}
echo '</div>';
$mysqli->close();
}
?>
<form action="signup.php" method="post">
Username:
<input type="text" size="20" name="username" />
<p/>
Password:
<input type="password" size="20" name="password" />
<p/>
Confirm Password:
<input type="password" size="20" name="cfmpassword" />
<p/>
Identify yourselves:
<select name="desig">
<option name="admin">Admin</option>
<option name="Faculty">Faculty</option>
<option name="Student">Student</option>
</select>
<p/>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
在你的情况下
'$inputError = false'
应该是
“$inputError==false”
只是少了一个
'='
这个怎么样$输入者!=trueif($inputError=false){$inputError=true;},现在,$inputError值为true,因为在if条件下,'$inputError=false'始终为true。因此,if($inputError!=true){//不工作}数据库
是SQL中的保留关键字。如果它是一个名字,你需要引用它。另外,如果您的问题仅围绕SQL查询,请不要转储整页代码。谢谢先生。我弄错了
'$inputError = false'