如果提供了合格令牌,则更新数据库表行
我的代码正常运行,但是此高效代码(例如16行)(评论)?我当时考虑使用$ row
并与上述变量进行比较,而不是编写另一个SQL查询。
我尝试使用一个变量和$ row ['field name']
,但是它丢了一个错误:
试图访问null类型的值
的阵列偏移
尝试访问类型NULL代码
<?php
require('../private/autoload.php');
if(isset($_GET['token'])){
$msg = "Email verified successfully, thank you.";
$token = $_GET['token'];
$email_status = "active";
$sql = "SELECT `email_token`, `email_status` FROM `users` where `email_token` = ? AND `email_status` = 'inactive' LIMIT 1";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $token);
$stmt->execute();
$result = $stmt->get_result();
$exist = $result->num_rows;
if($exist == 0 ){
// $row = $result->fetch_array(MYSQLI_ASSOC);
$sql = "SELECT `email_token`, `email_status` FROM `users` where `email_token` = ? AND `email_status` = ? LIMIT 1"; // Line 16
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $token, $email_status);
$stmt->execute();
$result = $stmt->get_result();
$exist = $result->num_rows;
if($exist == 1){
?>
<script>
alert("Email already verified.");
window.location = "../public/index.php";
</script>;
<?php exit(); ?>
<?php }else{ ?>
<script>
alert("User not found.");
window.location = "../public/index.php";
</script>;
<?php }
}else{
$sql = "UPDATE `users` SET `email_status`= ? where `email_token` = ? LIMIT 1";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $email_status, $token);
$stmt->execute();
$stmt->close();
$_SESSION['msg'] = $msg;
$_SESSION['token'] = $token;
header('Location: mobile_verify.php');
}
}else{
header('Location: index.php');
die();
}
$conn->close();
?>
My code works fine, but is this efficient code, like on line 16 (commented)? I was thinking to use $row
and compare with a variable mentioned above, rather than writing another SQL query.
I tried using a variable and $row['field name']
, but it was throwing an error:
Trying to access array offset on value of type null
Code
<?php
require('../private/autoload.php');
if(isset($_GET['token'])){
$msg = "Email verified successfully, thank you.";
$token = $_GET['token'];
$email_status = "active";
$sql = "SELECT `email_token`, `email_status` FROM `users` where `email_token` = ? AND `email_status` = 'inactive' LIMIT 1";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $token);
$stmt->execute();
$result = $stmt->get_result();
$exist = $result->num_rows;
if($exist == 0 ){
// $row = $result->fetch_array(MYSQLI_ASSOC);
$sql = "SELECT `email_token`, `email_status` FROM `users` where `email_token` = ? AND `email_status` = ? LIMIT 1"; // Line 16
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $token, $email_status);
$stmt->execute();
$result = $stmt->get_result();
$exist = $result->num_rows;
if($exist == 1){
?>
<script>
alert("Email already verified.");
window.location = "../public/index.php";
</script>;
<?php exit(); ?>
<?php }else{ ?>
<script>
alert("User not found.");
window.location = "../public/index.php";
</script>;
<?php }
}else{
$sql = "UPDATE `users` SET `email_status`= ? where `email_token` = ? LIMIT 1";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $email_status, $token);
$stmt->execute();
$stmt->close();
$_SESSION['msg'] = $msg;
$_SESSION['token'] = $token;
header('Location: mobile_verify.php');
}
}else{
header('Location: index.php');
die();
}
$conn->close();
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$ _ get
请求在服务器端执行“写作”过程,但是我想您您正在向用户发送电子邮件,而他们只是单击超链接,因此这是一种可容忍的情况。很简单,一个人执行更新查询,然后根据受影响的行的数量,在需要的情况下重定向。确保您是在需要维护会话的每个页面的开头开始会话。
未经测试的建议:
$_GET
request, but I am imagining that you are sending an email to the user and they are merely clicking a hyperlink so this is a tolerable scenario.Very simply, one execute an UPDATE query and then, depending on the number of affected rows, redirect where desired. Make sure that you are starting the session at the start of every page where the session needs to be maintained.
Untested recommendation:
建议是通过组合两者的各个方面来减少查询数量。用户不能同时“活动”或“非活动”,因此无需进行两个查询。
第一个查询已过时,因为所有信息都包含在第二个查询中。
(还请阅读初始帖子的评论)
The suggestion is to reduce the number of queries by combining the aspects of both. A user can't be "active" or "inactive" at the same time, so there is no need to do two queries.
The first query is obsolete, because all information is contained in the second query.
(please also read comments of the initial post)