如果所有记录都没有错误,php只会写入数据库

时间:2016-02-18 19:10:33

标签: php html mysql database pdo

附注:首先,我想说我不是一个经验丰富的网络开发人员,所以我所包含的代码中的任何内容都可以改进或做得更好请告诉我,因为我总是希望改善和学习那些比我更有经验和知识的人。

我正在创建一个表单向导类型的网站,用户可以通过各种表单进行处理并填写信息。在信息收集结束时,我将收集的数据写入在WAMP上运行的MySQL数据库。我正在编写的数据将转到数据库中的多个不同表。将始终有3个单独的INSERT查询,但最多约15个,具体取决于用户输入的数据(每个查询对应不同的表)。我遇到的问题是,如果任何INSERT语句有错误,它将不会写入该表(好),但它会写入所有其他表(当然)。

我希望能够做的只是在没有任何查询出错的情况下写入所有数据。如果任何查询有错误并且不会写入各自的表,我不想写任何数据并向用户显示错误消息。

到目前为止,我能够实现这一目标的唯一方法是使用PDO语句对象写入表,如果返回false,则使用$ pdostmt-> execute()catch。然后,当遇到失败(错误)时,追溯性地返回并删除已经写入的任何记录。这似乎不是一个很好的方法,我想在那里有人可以指出我更好的方式,但我还没有找到那种方式。

以下2个INSERT查询示例:

    $revisionUpdate = "INSERT INTO revision_history (stock_number, revision_date)"
                 ." VALUES ('"
                 .$dataLocation['stocknumber.tpl']['stocknum']."','"    //stock_number
                 .$timestamp  //revision_date
                 ."')";

    $updatestatement = $db_config_data->prepare($revisionUpdate);

    //  Execute the query to update table
    $updatestatement->execute();

    $infoUpdate = "INSERT INTO info (stock_number, revision_date, engineer, customer_name, model,"
                 ."above_one_gigabyte, installed_os, network_location, mac_dupe_check,"
                 ."time_local_tz, time_local_dst, time_offset, time_dst, configurator_version, stephen)"
                 ." VALUES ('"
                 .$dataLocation['stocknumber.tpl']['stocknum']."','"                            //stock_number
                 .$timestamp."','"                                                              //revision_date
                 .$dataLocation['general.tpl']['engineer']."','"                                //engineer
                 .$dataLocation['general.tpl']['company_name']."','"                            //customer_name
                 .$dataLocation['general.tpl']['parent_pn']."',"                                //model
                 .(int)($dataLocation['preflight.tpl']['tot_sys_memory'] == '1gb_greater').",'" //above_one_gigabyte
                 .$dataLocation['preflight.tpl']['installed_os']."','"                          //installed_os
                 .$dataLocation['general.tpl']['network_location']."',"                         //network_location
                 .(int)!array_key_exists('skip_MAC', $dataLocation['pxeactions.tpl']).",'"      //mac_dupe_check
                 .$dataLocation['timezone.tpl']['timezone']."',"                                //time_local_tz
                 .(int)array_key_exists('dst_adjust', $dataLocation['timezone.tpl']).",'"       //time_local_dst
                 .$dataLocation['timezone.tpl']['timezone']."',"                                //time_offset
                 .(int)array_key_exists('dst_adjust', $dataLocation['timezone.tpl']).",'"       //time_dst
                 ."1.0'"                                                                        //configurator_version
                 .")";

    $updatestatement = $db_config_data->prepare($infoUpdate);

    //Example of how I'd catch the failed INSERT query
    if ( !($updatestatement->execute()) )
    {
        print_r($updatestatement->errorInfo());
    }

因此,在上面的示例中,如果$ revisionUpdate成功且$ infoUpdate失败,我希望没有将$ revisionUpdate数据写入数据库,对于$ infoUpdate之前的任何其他查询也是如此。

感谢您的帮助。

0 个答案:

没有答案