0

In the function below after running the function its inserting into user table but wont insert into the usage table

function Signup($username, $password, $member) {
    global $dbs;
    try {
        // Generate API Key using JWT
        require_once 'JWT_class.php';
        $token = array();
        $token['id'] = uniqid(); // Generating a unique ID for API Key
        $apiKey = JWT::encode($token, 'secret_server_key');

        // Calculate Premium date based on member type
        $premiumDate = ($member == 1) ? date('Y-m-d', strtotime('+30 days')) : date('Y-m-d');

        $query = "INSERT INTO user (username, password, memberType, APIKey) VALUES (:username, :password, :memberType, :APIKey)";
        $statement = $dbs->prepare($query);
        $statement->bindValue(":username", $username);
        $statement->bindValue(":password", $password);
        $statement->bindValue(":memberType", $member);
        $statement->bindValue(":APIKey", $apiKey); // Insert API Key value
        $statement->execute();

        // Insert usage data into usage table
        $usageQuery = "INSERT INTO usage (APIKey, todaysUsage, UsageDate, PremiumDate) VALUES (:APIKey, 0, CURDATE(), :premiumDate)";
        $usageStatement = $dbs->prepare($usageQuery);
        $usageStatement->bindValue(":APIKey", $apiKey);
        $usageStatement->bindValue(":premiumDate", $premiumDate);
        $usageStatement->execute();



        $rowCount = $statement->rowCount();
        if ($rowCount < 0) {
            return true;
        } else {
            return false;
        }
    } catch (PDOException $ex) {
        // Handle the error gracefully, log, or display an appropriate message
        header("Location:../view/error.php?msg=" . $ex->getMessage());
    }
}

The database code is

CREATE TABLE `usage` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `APIKey` varchar(999) NOT NULL,
  `todaysUsage` int(11) NOT NULL,
  `UsageDate` date NOT NULL,
  `PremiumDate` date NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `APIKey_UNIQUE` (`APIKey`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

I tried debugging by adding the code below to the end but am not getting any visible output,

$usageStatement->execute();
$usageErrorInfo = $usageStatement->errorInfo();
if ($usageErrorInfo[0] !== '00000') {
    print_r($usageErrorInfo); // Debugging output
}
eglease
  • 2,445
  • 11
  • 18
  • 28
  • 2
    You're not checking any return values (e.g., `prepare()` or `execute()` might fail) and you're not catching any exceptions (like via `ERRMODE_EXCEPTION`), so it's not really much of a surprise that you're having silent issues. Also not sure what `if ($rowCount < 0) {` is supposed to accomplish... like, when would that ever be less than zero? – Alex Howansky Aug 29 '23 at 18:16
  • 1
    Also [errorInfo()](https://www.php.net/manual/en/pdo.errorinfo.php) is a method of `$dbs`, not of `$usageStatement`. This means you haven't switched on normal PHP error reporting and you didn't check the error logs. – KIKO Software Aug 29 '23 at 18:18
  • `usage` is a reserved term. Bad choice for table name. Will always need to backtick it. https://dev.mysql.com/doc/refman/8.0/en/keywords.html e.g. `INSERT INTO usage` is invalid, need `INSERT INTO \`usage\`` – user3783243 Aug 29 '23 at 18:34
  • @AlexHowansky They have `try/catch` around the code. – Barmar Aug 29 '23 at 19:19
  • @Barmar but if they did not configure the driver to throw exceptions in the first place, there might not be anything to catch, even if any of the queries goes wrong. And if the second query does not perform an insert as expected, but the code still returns true, that is probably the most likely explanation here. – CBroe Aug 30 '23 at 06:38

0 Answers0