2012-08-03 54 views
0

我想存儲POST數據到數據庫和我已動態生成的場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); 
    } 
} 

於是我嘗試了第二種方法:

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); 
    } 
} 

但在這種情況下,只有一個迭代運行,並在第二次迭代它給在循環的第二次迭代的一些錯誤。

錯誤在第二次迭代:

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 
     ) 
+0

第二次迭代中給出的誤差是多少? – 2012-08-03 14:55:27

+0

首先,不要使用'for'循環。使用'foreach'。其次,通過使用'var_dump()'調用來驗證變量的內容。 – Matt 2012-08-03 14:56:21

+0

你可以給我們一個'var_dump($ _ POST)'可能是什麼樣子的例子嗎? – ghbarratt 2012-08-03 15:01:17

回答

1

更新的var_dump

從的var_dump你可以看到,other_info不會有'other_info' => $other_infos[1]因爲只有一個數組中的項目。如果您希望秒碼正常工作,您可以向other_info添加另一個索引。

爲@JoseVega我上面的問題更新,錯誤

你錯誤

更新說明該值other_info爲空過去了,從錯誤中我會假設你的表架構不支持空值for other_info列。您可以更改模式以允許空值,也可以在空值時將值傳遞給它。

請嘗試以下操作並查看結果。我假設並非所有的數組都是相同的大小,這就是爲什麼你的第二次迭代失敗。

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); 
    } 
+0

這隻會隱藏潛在的問題(不張貼他期望他張貼的內容) – 2012-08-03 15:09:03

+0

@ErwinMoller我不認爲它是一個潛在的問題,我覺得other_info的價值不是必需的,當表單提交other_info爲空,因此導致他正在看到的錯誤。它是一個確保值被設置的情況,並且如果它不傳遞除null以外的其他東西。 – 2012-08-03 15:14:19

+0

@JoseVega在查看var_dump後感謝,我得到了腳本中的語法錯誤。謝謝 – Rajul 2012-08-03 15:23:36

1

$ _ POST [ '當然'] < - 包含數組,如你所說。

所以,如果你會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" 
    } 

但在你的第一個代碼,例如,你使用:

$這個 - >輸入 - >後,

哪些沒有按( '課程' $ I)。不存在(course12345不存在)。

在你的第二個代碼示例,你這樣做是正確的:

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

現在$課程持有數組,你似乎在你的不經意檢查循環使用權(也許我錯過了什麼)。

我懷疑你最好的開始調試用一個簡單的

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

,並非常肯定您收到有望獲得什麼。 我懷疑你不會發布你認爲你發佈的所有內容。

確保您在所有使用的陣列中具有與您的課程陣列中一樣多的元素。

如果沒有看到$ _POST的實際內容,很難提供更多建議。

+0

@Ervin我已經更新var_dump在上面的問題 – Rajul 2012-08-03 15:15:06

+0

我明白了。 Jose Vega已經放大了你的問題,他是對的,所以我把你的問題留給他,然後繼續前進。祝你好運! – 2012-08-03 15:20:09

相關問題