【发布时间】:2020-04-11 07:52:35
【问题描述】:
我正在创建一个验证码系统,用户收到一封带有验证码的电子邮件,他们会去我的网站,输入它,如果有代码与数据库表“验证码”中的代码匹配,那么他们会被重定向到另一个网址。但是到目前为止,我输入的每个代码都是 testsite.php?code=not matching。我做错了什么?
<?php
if (isset($_POST['code-submit'])) {
require 'notseendatabasehandler.php';
$code = $_POST['code'];
//Checking for empty fields
if (empty($code)) {
header("Location: testsite1.php?error=emptyfields");
exit();
}
else {
$sql = "SELECT codeNumber FROM verificationcodes;";
$result = mysqli_query($conn, $sql);
if ($result == $code) {
header("Location: testsite2.php?code=success");
exit();
}
else {
header("Location: testsite1.php?code=notmatching");
exit();
}
}
}
//Sending the user backwards if they entered incorrectly
else {
header("Location: homepage.php");
exit();
}
【问题讨论】:
-
您确定
$result或$code包含它应该包含的内容吗? -
mysqli_query只是运行查询。然后,您需要使用$row = mysqli_fetch_assoc($result);之类的方法获取结果,然后将$row['codeNumber']与$code进行比较。此外,您需要在查询中添加WHERE子句,理想情况下,您应该更改为准备好的语句以避免 SQL 注入。见stackoverflow.com/questions/60174/…