简体   繁体   English

验证后插入MySQL

[英]MySql insert after validation

How can I add a MySQL insert if there are no errors caught? 如果没有发现错误,如何添加MySQL插入? Here's my code: 这是我的代码:

foreach($data as $key => $value){

    try{
        $rules[$key]->assert($value);
    } catch (\InvalidArgumentException $e) {
        $errors = $e->findMessages([
        'email' => 'Email must be a valid email address.',
        'equals' => 'Both passwords must match.']);
        echo $e->getFullMessage().'<br />';
    }

}

I've tried adding the code after the catch but that executes it several times because it's inside a loop. 我试过在捕获之后添加代码,但是由于它在循环内,所以执行了几次。

Placing the code outside of the loop causes it to always execute. 将代码放置在循环之外会导致其始终执行。

If no errors were thrown, the code keeps being interpreted. 如果未引发任何错误,则代码将继续被解释。 So you should just add the code you want after the catch , like so: 因此,您应该只在catch后面添加所需的代码,如下所示:

foreach($data as $key => $value){

    try{
        $rules[$key]->assert($value);
    } catch (\InvalidArgumentException $e) {
        $errors = $e->findMessages([
        'email' => 'Email must be a valid email address.',
        'equals' => 'Both passwords must match.']);
        echo $e->getFullMessage().'<br />';
    }

    // your code here

}

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

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