gpt4 book ai didi

php - 这是try-catch-finally的可接受用法吗?

转载 作者:行者123 更新时间:2023-12-03 08:24:09 25 4
gpt4 key购买 nike

基本上,我正在寻找一些说明,这是可以尝试使用的方法吗?基于表格中电子邮件和用户名的唯一性

try
{
$db = new Database;
$success = ["message" => "Please check your Email address to activate your account"];
$process = $db->prepare('INSERT INTO users (username, email, password, profileImg, profileImgPath, profileImgType, accountStatus, verified, joined)
VALUES
(:username, :email, :password, :filename, :filepath, :filetype, :activationCode, 0, NOW())');
$process->bindValue(':username', $username);
$process->bindValue(':email', $email);
$process->bindValue(':password', password_hash($post['password'], PASSWORD_DEFAULT));
$process->bindValue(':activationCode', $activationCode);
$process->bindValue(':filename', $filename);
$process->bindValue(':filepath', $filepath);
$process->bindValue(':filetype', $filetype);
$process->execute();
$code = 'https://gotsocial.co.uk/gotsocial/active.php?activecode=' . $activationCode . '.
';
$to = $post['email'];
$subject = 'GOT Social';
$from = "register@gotsocial.co.uk";
$result = mail($to, $subject, $code, "From: $from");
}
catch(Exception $e)
{
$errors[] = ["name" => "username", "error" => "Username may already be taken"];
}
catch(Exception $e)
{
$errors[] = ["name" => "email", "error" => "Email may already be registered"];
}
finally
{
header("refresh:10; url=index.php");
}

最佳答案

不,您不能两次输入相同的catch(Exception $e)。如果需要具有不同的捕获,则必须更改异常的类型,否则必须将它们合并为一个

try
{
$db = new Database;
$success = ["message" => "Please check your Email address to activate your account"];
$process = $db->prepare('INSERT INTO users (username, email, password, profileImg, profileImgPath, profileImgType, accountStatus, verified, joined)
VALUES
(:username, :email, :password, :filename, :filepath, :filetype, :activationCode, 0, NOW())');
$process->bindValue(':username', $username);
$process->bindValue(':email', $email);
$process->bindValue(':password', password_hash($post['password'], PASSWORD_DEFAULT));
$process->bindValue(':activationCode', $activationCode);
$process->bindValue(':filename', $filename);
$process->bindValue(':filepath', $filepath);
$process->bindValue(':filetype', $filetype);
$process->execute();
$code = 'https://gotsocial.co.uk/gotsocial/active.php?activecode=' . $activationCode . '.
';
$to = $post['email'];
$subject = 'GOT Social';
$from = "register@gotsocial.co.uk";
$result = mail($to, $subject, $code, "From: $from");
}
catch(Exception $e)
{
$errors[] = ["name" => "username", "error" => "Username may already be taken"];
$errors[] = ["name" => "email", "error" => "Email may already be registered"];
}
finally
{
header("refresh:10; url=index.php");
}

关于php - 这是try-catch-finally的可接受用法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47234869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com