简体   繁体   English

如果 PDO 数据库更新成功则重定向页面

[英]Redirect page if PDO database update is success

Here is a part of the code I am using to update the database.这是我用来更新数据库的代码的一部分。 It works, but I would like to redirect the user to another page if the database update is a success.它有效,但如果数据库更新成功,我想将用户重定向到另一个页面。 How can I do that?我怎样才能做到这一点? I know the header('Location: ../../');我知道header('Location: ../../'); , but where to use it? ,但是在哪里使用呢?

$statement = $dbconnect->prepare("
   UPDATE members 
   SET name = :fname, lastname = :lname, phone = :phone 
   WHERE member_id = :memberid
");
$dbconnect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$statement->execute(
   array(
      ':fname' => "$fname", 
      ':lname' => "$lname", 
      ':phone' => "$phone",
      ':memberid' => "$memberid"
   )
);
} catch(PDOException $e) {
    die("ERROR: Could not connect. " . $e->getMessage());
}

Even tried the following, but no redirection甚至尝试了以下,但没有重定向

    $statement->execute(array(':fname' => "$fname", ':lname' => "$lname", ':phone' => "$phone",':memberid' => "$memberid"));

if ($statement) {
   header('Location: http://sitename.com');
} else {
   echo 'It failed!';
}

You need to check the result from the PDOStatement::execute execution and use the correct binding when you execute a prepared statement with an array of insert values (named parameters).您需要检查PDOStatement::execute执行的结果,并在执行带有插入值数组(命名参数)的准备好的语句时使用正确的绑定。 Note, that the result from the successful PDO::prepare call is a PDOStatement object, not a boolean value.请注意,成功的PDO::prepare调用的结果是PDOStatement object,而不是 boolean 值。

The folloling script, based on your code, is a possible solution to your problem:基于您的代码的以下脚本是您问题的可能解决方案:

<?php

try {

    $dbconnect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $statement = $dbconnect->prepare("
        UPDATE members 
        SET name = :fname, lastname = :lname, phone = :phone 
        WHERE member_id = :memberid
    ");
    if ($statement === false) {
       die("ERROR: Could not prepare statement.");
    }

    $result = $statement->execute(
        array(
            ':fname' => $fname, 
            ':lname' => $lname, 
            ':phone' => $phone,
            ':memberid' => $memberid
        )
    );
    if ($result) {
        header('Location: http://sitename.com');    
        exit;
    }   
    
} catch(PDOException $e) {

    die("ERROR: Could not connect. " . $e->getMessage());

}

?>

If you want to assume that your update was successful as long as you did not encounter any PDO errors (I assume that updates without errors are successful) you can write this:如果你想假设你的更新是成功的,只要你没有遇到任何 PDO 错误(我假设没有错误的更新是成功的)你可以这样写:

$statement = $dbconnect->prepare("UPDATE members SET name = :fname, lastname = :lname, phone = :phone WHERE member_id = :memberid");
$dbconnect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$statement->execute(array(':fname' => "$fname", ':lname' => "$lname", ':phone' => "$phone",':memberid' => "$memberid"));

// if there were no errors redirect now
header('Location: ../../');


   } catch(PDOException $e){
    die("ERROR: Could not connect. " . $e->getMessage());
}

On another note outside of dev you should find a different way to handle the error: this is almost the same as not having a try/catch.在 dev 之外的另一个注意事项中,您应该找到一种不同的方法来处理错误:这与没有 try/catch 几乎相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM