PHP 准备好的语句更新不起作用 [关闭]

问题描述 投票:0回答:0

我正在尝试使用准备好的语句从 PHP 脚本更新 MySQL 表,但它没有更新任何内容。

根据众神的说法,这是关闭的,因为它是由错字引起的,但没有一个评论员以任何方式证明了这一点。

完整的 PHP 脚本在这里:


    <?php
// ================================
// FILE NAME: activate.php
//
// SUMMARY: Activates a new account 
//
// CALLED: from a link in an email sent
// to the new user from register_db.php
//
// TABLE:
// CREATE TABLE IF NOT EXISTS Users (
//   ID int NOT NULL AUTO_INCREMENT,
//   GivenName varchar(50) NOT NULL,
//   FamilyName varchar(50)NOT NULL,
//   Email varchar(255) NOT NULL,
//   Password char(60) NOT NULL,
//   Active boolean not null DEFAULT 0,
//   Code char(20) NOT NULL,
//   Activated datetime DEFAULT NULL,
//   Created timestamp NOT NULL DEFAULT current_timestamp(),
//   Updated datetime DEFAULT current_timestamp() ON UPDATE current_timestamp(),
//   CONSTRAINT user_pk PRIMARY KEY (ID),
//   CONSTRAINT email_unique UNIQUE (Email)
//  ) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;
//
// HOW: a user clicks a link which contains the 
// URL for this page, along with an encrypted
// email address and a hashed activation code
//
// Further actions are carried out by login_db.php
//
// EXCEPTIONS
// If no user with these credentials can be retrieved 
// then the account has already been activated or deleted.
// If the activation period has expired then
// delete the account and re-direct to the 
// rgistration page
// ================================

//include at top of your script
 session_start();
// This prints errors if page cannot load
ini_set('display_errors', 1);
// Utilise the database connection file
include "../db/dws_db.php";
// Utilise the encryption/decryption file
include  "../db/endecrypt.php";

// If link in URL get the email and hash
if(
    !empty($_GET["email"]) 
    AND !empty($_GET["code"])
    )
{
    // Email and code are both in the URL
    $email = $_GET["email"];
    $code = $_GET["code"];
} 

// https://phpdelusions.net/mysqli_examples/prepared_select
// Next get user data from the database
$sql = "SELECT GivenName, Created
            FROM Users 
            WHERE Code = ? AND Active = 0";
$stmt1= $db_connection->prepare($sql); 
$stmt1->bind_param("s", $code);
$stmt1->execute();
// Extract the retrieved data
$stmt1->bind_result($given, $created);
// Check is any data is returned - if none this is due
// to user having been already activated
if( $stmt1->fetch())
    {
        $stmt1->close();
    } else
    {
        $_SESSION["msg"] = "Already activated - please log in";
        header('Location: login.php', true, 303);
    }

// Check if expiry not passed
$now = date("Y/m/d H:i");
$c =strtotime($created);
$c = date("Y/m/d H:i", $c);
// Check if link has expired
if ($now  - $c < 1)
    {
        // Link is still valid
        $valid = true;
    } else 
    {
        $valid = false;
    }

if($valid )
    {
        // Flag activated
        $active = 1;
        // The UPDATE SQL
        $sql =  "UPDATE Users 
                    SET Active = ?, Activated = ? 
                    WHERE Email = ?";
        ($stmt2 = $db_connection->prepare($sql)) or trigger_error($db_connection->error, E_USER_ERROR);
        // This used to raise an error - now I get blank screen
        $stmt2->bind_param("iss", $active,  $now, $email);
        $stmt2->execute() or trigger_error($stmt2->error, E_USER_ERROR);
        // 0 ROWS UPDATED
        printf("Rows updated: %d\n", $stmt2->affected_rows);
        
        
    } else 
    {
        // Do other stuff when i get UPDATE working
    }   
?>

我已经使用正确的值在 Workbench 中执行了更新并且它有效;如果我用这些值在 PHP 脚本中重写更新,它就可以工作;我已经完成了 var_dump() 来检查所有变量是否已正确初始化。

我不再收到任何错误,但是 UPDATE 什么都不做,如行所示

printf("Rows updated: %d\n", $stmt2->affected_rows);

我已经无计可施了。

php mysqli
© www.soinside.com 2019 - 2024. All rights reserved.