0

I am currently working on a project where I need to get a user to sign up and then an email is sent to the website administrator's email with an approval link (containing the php page below, user email and sha512). When the approval link is clicked, it's meant to Update the table with the corresponding email and change isApproved to 1, but it does nothing other than printing the echos.

I tried changing the SQL commands, adding the ` around the names, looking it up on w3schools, stackoverflow and other forums and found nothing.

<?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");
    }
?>

All I see when opening the website is "approved", which is what i expected, but the records in the database remain unchanged.

Jelon51
  • 3
  • 2

2 Answers2

3

That's because you are not running the query statement.

$sql = "UPDATE `User` SET `isApproved`='1' WHERE `User`.`email`=$email"; is a query which you wrote but did not executed it. You need to execute it using mysqli_query() method passing in the connection and query in parameters mysqli_query($conn, $sql);. You can read more about this here.

Updated Code:

   <?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";
            mysqli_query($conn, $sql); // <------- Run SQL Query 
            echo("approved");
        }
    ?>
Community
  • 1
  • 1
Javapocalypse
  • 2,223
  • 17
  • 23
0

I think you didn't run execute command.

    $conn = mysqli_connect($host, $dbUsername, $dbPassword, $dbname);
    ....
    $sql = "UPDATE `User` SET `isApproved`='1' WHERE `User`.`email`=$email";
    $stmt = mysqli_prepare($conn, $sql);
    mysqli_stmt_execute($stmt);
kbin0505
  • 170
  • 1
  • 11