当且仅当没有错误时,使用foreach验证PHP表单并存储值

当且仅当没有错误时,使用foreach验证PHP表单并存储值,php,validation,foreach,Php,Validation,Foreach,我有一个for字段数组,如果我未经验证就提交,则将数据保存到数据库中不会出现问题 我遇到的麻烦是,在使用update\u设置($optionname,$optionvalue)保存任何单个字段数据之前,检查foreach迭代中是否存在错误 当前代码正在保存除错误字段外的所有字段的数据。所以,若并没有单个错误,是否有任何方法可以首先验证所有字段并只存储到数据库中。否则,在页面上显示错误消息 $errors = []; foreach ( $optionnames as $optionname )

我有一个for字段数组,如果我未经验证就提交,则将数据保存到数据库中不会出现问题

我遇到的麻烦是,在使用
update\u设置($optionname,$optionvalue)保存任何单个字段数据之前,检查foreach迭代中是否存在错误

当前代码正在保存除错误字段外的所有字段的数据。所以,若并没有单个错误,是否有任何方法可以首先验证所有字段并只存储到数据库中。否则,在页面上显示错误消息

$errors = [];
foreach ( $optionnames as $optionname ) {
    $optionvalue = get_post_field( $optionname );

    // check the field if not set
    if ( get_post_field( 'test' ) == '' ) {
        $errors['test'] = 'Test field is required';
    }

    /**
     * all loop items only should be add/update if there is not single error
     * If any single error occure than it shold not save any single field data
     */
    // add/update settings
    elseif ( empty( $errors ) ) {
        update_setting( $optionname, $optionvalue );
    }
}
琐碎但可能有用:)


与其每次迭代都使用更新查询来访问数据库,为什么不只运行一个查询呢?无论如何,如果您将代码分成两个foreach循环,您就可以实现目标,如下所示:

// first loop validates each option
$errors = [];
foreach($optionNames as $optionName){
     $optionValue = get_post_field($optionName);

     if( strlen(trim($optionValue)) == 0 ){
         $errors[] = sprintf("%s field is required!", $optionName);
         // you could break here if you do not want to accumulate error messages
     }
}

// if any errors were found, halt execution and output error messages
if( count($errors) > 0){
   $errorMessages = implode(",", $errors);
   die("Cannot save due to the following errors: " . $errorMessages);
}

// this will only execute if no errors were found
foreach($optionNames as $optionName){
    $optionValue = get_post_field($optionName);
    update_setting( $optionName, $optionValue );
}
我不会这样做,但我选择使用您提供的代码来回答您的问题,而不是提供完全不同的内容


在选择提前返回(或者在我的示例中,停止执行)的情况下,尽量避免使用“else”。它通过提供一条通向所需结果的清晰路径来帮助清理代码

foreach($info作为数据){if(validation($info)){//sql insert}else{echo“error on$info”;}
另一种方法:
foreach($info作为数据){if(validation($info)){$validInfo[]=$info;}else{$badInfo[]=$info;foreach($validInfo作为$info){//sql insert}
谢谢,让我试试第二种选择。如果你成功了,请告诉我。很好。我正在尝试。谢谢。很快会回来的
// first loop validates each option
$errors = [];
foreach($optionNames as $optionName){
     $optionValue = get_post_field($optionName);

     if( strlen(trim($optionValue)) == 0 ){
         $errors[] = sprintf("%s field is required!", $optionName);
         // you could break here if you do not want to accumulate error messages
     }
}

// if any errors were found, halt execution and output error messages
if( count($errors) > 0){
   $errorMessages = implode(",", $errors);
   die("Cannot save due to the following errors: " . $errorMessages);
}

// this will only execute if no errors were found
foreach($optionNames as $optionName){
    $optionValue = get_post_field($optionName);
    update_setting( $optionName, $optionValue );
}