0

MySQL と XML を使用してエクスポート/インポート機能を作成しようとしています。データは通常、ある登録ユーザーによってエクスポートされ、別のユーザーによってデータベースにインポートされます。

データは異なるテーブルに存在し、エクスポート クエリの SQL "where" 句で指定された ID に関連付けられています。

結合を使用して SQL クエリを実行することで、データを 1 つのファイルにエクスポートできます。

他のユーザーがエクスポートされたファイルからデータをインポートできるようにする「最も適切な」方法は何ですか?

私の問題は、ファイル内のレコードに特定のテーブルでのみ使用されるフィールドが含まれており、データが挿入されているテーブルで使用されていないフィールドを参照すると、SQL クエリが失敗することです。

挿入するフィールドを指定するクエリを作成することでこれを回避できますが、アプリケーションの開発中にインポート クエリに対するこれらのメンテナンスの更新を見落とすと、メンテナンスのオーバーヘッドが大きくなり、脆弱性が発生します。

これは「ベスト プラクティス」の質問です。

アップデート

リンクされたテーブルと自動インクリメント プライマリ ID の抽出と挿入に関連する問題を克服するために私が最終的に行ったことは、行をそのままエクスポートすることでした。つまり、プライマリ ID を含むすべてのフィールドがエクスポート ファイルに追加されました。次に、データをインポートし直すときに、リンクされたテーブルから行を正しく追加するために、私のコードは異なるテーブルからすべての古い ID を保存し、これらの古い ID なしで行を挿入しました。これには、古い ID を使用して、異なるテーブルのデータを適切にリンクすることが含まれていました。新しい ID は自動インクリメントによって生成されます。これは少し複雑でしたが、少なくとも id 以外のフィールドは処理されませんでした。これにより、アプリケーション開発中に新しいデータ フィールドが追加されたときに、コードがより堅牢になり、破損しにくくなります。

私のコードを以下に示します。$file はダイアログを使用して作成されることに注意してください。

    if($option=='Export test'){
        // ----
        // Exports an xml file. This includes all primary ids
        // ----
        $file='export.xml';
        $xml='<?xml version="1.0" encoding="ISO-8859-1" ?>'."\n";
        $xml.="<".$database_connect.">\n";
        file_put_contents($file,$xml);
        $rows=returnTestQuestionsPlus($template_id);
        $table='test_questions';
        exportRowsToXMLFile($table,$rows,$file);
        foreach($rows as $row){
            $question_id=$row['question_id'];
            $rows2=returnTestAnswersPlus($question_id);
            $table='test_answers';
            exportRowsToXMLFile($table,$rows2,$file);
        }
        $xml="</".$database_connect.">";
        file_put_contents($file,$xml,FILE_APPEND);
        header("Location: select_template.php?info_message=The $object6 has been exported to $file.");
        exit;
    }
    if($option=='Import test'){
        // ----
        // Gets xml file content. This includes all primary ids.
        // ----
        $file='export.xml';
        $xml=file_get_contents($file);
        $xml=str_ireplace(chr(10),'',$xml);
        $xml=str_ireplace(chr(13),'',$xml);
        $xml=new SimpleXMLElement($xml);
        $tables = json_decode(json_encode((array)$xml), TRUE);
        // ----
        // Removes primary ids and then inserts data into the database.
        // ----
        foreach($tables as $table=>$rows){
            foreach($rows as $row){
                if($table=='test_questions'){
                    $old_question_ids[]=$row['question_id'];
                }
            }
        }
        foreach($tables as $table=>$rows){
            foreach($rows as $row){
                if($table=='test_questions'){
                    $question_id=$row['question_id'];
                    unset($row['question_id']);
                    unset($row['sort_id']);
                    unset($row['template_id']);
                    $sql="select count(*) as number from test_questions";
                    $row2=returnRow(__LINE__,$sql);
                    extract($row2);
                    $sort_id=$number+1;
                    $row['sort_id']=$sort_id;
                    $row['template_id']=$template_id;
                    $results=insertRow2('test_questions',$row);
                    extract($results);
                    $new_question_ids[]=$id;
                }
            }
        }
        $test_answers_rows=array();
        foreach($tables as $table=>$rows){
            foreach($rows as $row){
                if($table=='test_answers'){
                    $question_id=$row['question_id'];
                    foreach($old_question_ids as $key=>$old_question_id){
                        if($question_id==$old_question_id)$new_question_id=$new_question_ids[$key];
                    }
                    unset($row['question_id']);
                    unset($row['answer_id']);
                    $row['question_id']=$new_question_id;
                    $test_answers_rows[]=$row;
                }
            }
        }
        insertMultipleRowsPDP('test_answers',$test_answers_rows);
        header("Location: select_template.php?info_message=The $object6 has been imported from $file.");
        exit;
    }
4

0 に答える 0