0

投稿データをデータベースに保存したいのですが、動的に生成されたフィールドcourse[]などがstart_date[]あります。次のコードを記述して、投稿データを取得して保存します。

ただし、「0」以下の実行後は、すべてのフィールドにゼロが格納されます。

if($this->input->post('course'))
{

    $education_data= array();
    for($i=0; $i<count($_POST['course']); $i++)
    {
        $education = array(
                'course'       => $this->input->post('course' . $i),
                'start_date'   => $this->input->post('start_date' . $i),
                'end_date'     => $this->input->post('end_date' . $i),
                'college_name' => $this->input->post('college_name' . $i),
                'other_info'   => $this->input->post('other_info' . $i),
        );

        $this->db->insert('education',$education);
    }
}

だから私は2番目の方法を試しました:

if($this->input->post('course'))
{    

    $courses = $this->input->post("course");
    $startdates = $this->input->post("start_date");
    $end_date = $this->input->post("end_date");
    $college_name = $this->input->post("college_name");
    $other_infos   = $this->input->post("other_info");

    $education_data= array();
    for($i=0; $i<count($_POST['course']); $i++)
    {

        $education = array(
                 'course'       => $courses[$i],
                 'start_date'   => $startdates[$i],
                 'end_date'     => $end_date[$i],
                 'college_name' => $college_name[$i],
                 'other_info'   => $other_infos[$i],    //line number 101
        );

        // Insert Education data in 'education' table
        $this->db->insert('education',$education);
    }
}

ただし、この場合、1回の反復のみが実行されており、2回目の反復では、ループの2回目の反復でエラーが発生しています。

2 回目の反復でのエラー:

Severity: Notice

Message: Undefined offset: 1

Filename: models/resume_model.php

Line Number: 101


A Database Error Occurred

Error Number: 1048

Column 'other_info' cannot be null

INSERT INTO `education` (`course`, `start_date`, `end_date`, `college_name`, `other_info`) VALUES ('', '', '', '', NULL)

Filename: C:\wamp\www\resume\system\database\DB_driver.php

Line Number: 330

誰かが知っている場合は、解決策を提案してください。

var_dump 出力:

 [course] => Array
        (
            [0] => course1
            [1] => course2
        )

    [start_date] => Array
        (
            [0] => from1
            [1] => from2
        )

    [end_date] => Array
        (
            [0] => to1
            [1] => to2
        )

    [college_name] => Array
        (
            [0] => univ1
            [1] => univ2
        )

    [other_info] => Array
        (
            [0] => other1
        )
4

2 に答える 2

1

var_dump の更新

'other_info' => $other_infos[1]var_dump から、配列に項目が 1 つしかないため、other_info に a がないことがわかります。秒のコードを正しく動作させたい場合は、other_info に別のインデックスを追加できます。

@JoseVegaの更新上記の質問をエラーで更新しました

あなたのエラーは、other_info に渡された値が null であると述べています。このエラーから、テーブル スキーマは other_info 列の null 値をサポートしていないと思います。null 値を許可するようにスキーマを変更するか、null の場合に値を渡すことができます。

以下を試して、結果を確認してください。すべての配列が同じサイズであるとは限らないため、2 回目の反復が失敗すると想定しています。

    for($i=0; $i<count($courses); $i++) {

         $education = array(
                    'course'       => isset($courses[$i]) ? $courses[$i] : "",
                    'start_date'   => isset($startdates[$i]) ? $startdates[$i] : "",
                    'end_date'     => isset($end_date[$i]) ? $end_date[$i] : "",
                    'college_name' => isset($college_name[$i]) ? $college_name[$i] : "",
                    'other_info'   => isset($other_infos[$i]) ? $other_infos[$i] : "",
                    );

         // Insert Education data in 'education' table
         $this->db->insert('education',$education);
    }
于 2012-08-03T15:01:33.630 に答える
1

$_POST['course'] <-- あなたが述べたように、配列が含まれています。

したがって、print_r($POST); 次のように表示されます。

array(2) {
  ["course"]=>
  array(3) {
    [0]=>
    string(5) "17740"
    [1]=>
    string(5) "18422"
    [2]=>
    string(5) "14170"
  }
  ["startdate"]=>
  array(3) {
    [0]=>
    string(10) "2012-01-02"
    [1]=>
    string(10) "2012-01-02"
    [2]=>
    string(10) "2012-01-02"
  }

ただし、最初のコード例では次を使用します。

$this->input->post('course' . $i),

これは存在しません (course12345 は存在しません)。

2 番目のコード例では、正しく実行します。

$courses = $this->input->post("course");

これで、$courses に配列が保持されます。これは、通常の検査でループ内で使用しているように見えます (何かを見落としている可能性があります)。

簡単な方法でデバッグを開始するのが最善だと思います

echo "<pre>";
print_r($_POST);
echo "</pre>";

そして、受け取ると思っていたものを確実に受け取ってください。投稿していると思われるものすべてを投稿しているとは思えません。

使用するすべての配列に、コース配列と同じ数の要素があることを確認してください。

$_POST の実際の内容を見ずにこれ以上アドバイスすることは困難です。

于 2012-08-03T15:05:12.277 に答える