猿问

用户通过电子邮件批准显示输出但不更新数据库中的记录

我目前正在做一个项目,我需要让用户注册,然后向网站管理员的电子邮件发送一封带有批准链接的电子邮件(包含下面的 php 页面、用户电子邮件和 sha512)。单击批准链接时,它意味着使用相应的电子邮件更新表并将 isApproved 更改为 1,但它除了打印回声之外什么都不做。


我尝试更改 SQL 命令,在名称周围添加 `,在 w3schools、stackoverflow 和其他论坛上查找,但一无所获。


<?php

    $hash = $_GET['h'];

    $email = $_GET['e'];


    if($hash == hash('sha512', 'ACCEPT')){

    $host = "redacted";

    $dbUsername = "redacted";

    $dbPassword = "redacted";

    $dbname = "redacted";

    //create connection

    $conn = mysqli_connect($host, $dbUsername, $dbPassword, $dbname);

    if (mysqli_connect_error()) 

    {

        die('Connect Error('. mysqli_connect_errno().')'.mysqli_connect_error());

    } 

    else 

    {

        $sql = "UPDATE `User` SET `isApproved`='1' WHERE `User`.`email`=$email";

        echo("approved");

    }

?>

我打开网站时看到的都是“已批准”,这正是我所期望的,但数据库中的记录保持不变。


慕少森
浏览 150回答 2
2回答

猛跑小猪

那是因为您没有运行查询语句。$sql = "UPDATE `User` SET `isApproved`='1' WHERE `User`.`email`=$email";是您编写但未执行的查询。您需要使用 mysqli_query() 方法在连接中传递并在参数中查询来执行它mysqli_query($conn, $sql);。您可以在此处阅读更多相关信息。更新代码:&nbsp; &nbsp;<?php&nbsp; &nbsp; &nbsp; &nbsp; $hash = $_GET['h'];&nbsp; &nbsp; &nbsp; &nbsp; $email = $_GET['e'];&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if($hash == hash('sha512', 'ACCEPT')){&nbsp; &nbsp; &nbsp; &nbsp; $host = "redacted";&nbsp; &nbsp; &nbsp; &nbsp; $dbUsername = "redacted";&nbsp; &nbsp; &nbsp; &nbsp; $dbPassword = "redacted";&nbsp; &nbsp; &nbsp; &nbsp; $dbname = "redacted";&nbsp; &nbsp; &nbsp; &nbsp; //create connection&nbsp; &nbsp; &nbsp; &nbsp; $conn = mysqli_connect($host, $dbUsername, $dbPassword, $dbname);&nbsp; &nbsp; &nbsp; &nbsp; if (mysqli_connect_error())&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; die('Connect Error('. mysqli_connect_errno().')'.mysqli_connect_error());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sql = "UPDATE `User` SET `isApproved`='1' WHERE `User`.`email`=$email";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mysqli_query($conn, $sql); // <------- Run SQL Query&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo("approved");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ?>

墨色风雨

我想你没有运行执行命令。&nbsp; &nbsp; $conn = mysqli_connect($host, $dbUsername, $dbPassword, $dbname);&nbsp; &nbsp; ....&nbsp; &nbsp; $sql = "UPDATE `User` SET `isApproved`='1' WHERE `User`.`email`=$email";&nbsp; &nbsp; $stmt = mysqli_prepare($conn, $sql);&nbsp; &nbsp; mysqli_stmt_execute($stmt);
随时随地看视频慕课网APP
我要回答