im正在为学校的一个项目创建一个网站。第一步应该是简单的-寄存器&登录页面。php代码从未工作过。。。(有时是由于代码中的问题,大多数时候结果都是空白页)。
html代码:
<!doctype html>
<html>
<title>Tracycle Registration</title>
<link rel="stylesheet" href="test.css">
<div id="header">
<h1>Tracycle</h1>
</div>
<br>
<h3 align="center">Register with your email address</h3>
<br>
<form align="center" action="register.php" method="post">
<input type="email" id="textboxid" placeholder="Email" name="email" size="50" required>
<br><br>
<input type="password" id="textboxid" placeholder="Password" name="password" size="50" required>
<br><br>
<center><input type="submit" name="register" value="Register" ></center>
</form>
</html>
php代码:
<?php
$host = "127.0.0.1";
$user = "tracycle";
$pass = "";
$db = "users";
$port = 3306;
$connection = mysqli_connect($host, $user, $pass, $db, $port)or die(mysql_error());
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$check_email = "SELECT * FROM users_information WHERE email='$email";
$run = mysql_query($check_email);
if(mysql_num_rows($run)>0)
{
echo "This email already exists!";
}
$query = "INSERT INTO users_information (email, password) VALUES ('$email', '$password')";
if(mysql_query($query))
{
echo "Success!";
}
}
?>
BTW im使用cloud9开发环境
你知道问题出在哪里吗?为什么结果是空白页?(填写html表单并单击提交按钮后)我很想得到帮助:)
if(isset($_POST['submit']))
必须是
if(isset($_POST['register']))
尝试激活显示PHP错误:如何显示PHP错误?
"php和html注册页面结果为空白页面"
空白页意味着您有语法错误,并且代码中有许多错误。
首先,if(isset($_POST['submit'])){...}
的条件语句永远不会发生,因为提交按钮具有register
名称属性,而不是submit
。
将以下内容添加到您当前的代码中,并且就在您关闭?>
标签之前,将具有回显Submit is not set.
else{
echo "Submit is not set.";
}
那么WHERE email='$email";
中缺少一个引号,这将导致语法错误。
您正在连接mysqli_
API,但使用mysql_
函数。
来自不同MySQL API的不同函数不会混用
从连接到查询,都必须使用相同的方法。
请参阅以下链接:
- 我可以在PHP中混合MySQL API吗
- http://php.net/manual/en/function.error-reporting.php
- http://php.net/manual/en/mysqli.error.php
- http://php.net/manual/en/mysqli.query.php
- http://php.net/manual/en/mysqlinfo.api.choosing.php
在检查错误时,会产生相当多的错误,其中之一是未定义的索引提交通知。
您当前的代码对SQL注入是开放的。将mysqli_*
与准备好的语句一起使用,或将PDO与准备好的报表一起使用
密码
我还注意到,您可能将密码存储为纯文本。不建议这样做。
使用以下选项之一:
- CRYPT_BLOWFISH
crypt()
bcrypt()
scrypt()
- 在OPENWALL上
- PBKDF2
- PHP.net上的PBKDF2
- PHP 5.5的
password_hash()
函数 - 兼容性包(如果PHP<5.5)https://github.com/ircmaxell/password_compat/
其他链接:
- PBKDF2用于PHP
HTML粘贴器
您的HTML还包含无效标记。
应该有带有样式表和标题声明的<head></head>
标记,以及用<body></body>
标记包装的HTML。
<!doctype html>
<html>
<head>
<title>Tracycle Registration</title>
<link rel="stylesheet" href="test.css">
</head>
<body>
<div id="header">
...
</form>
</body>
</html>