2015-07-06 92 views
0

試圖從textarea的行添加到mysql數據庫,只是最後一排正確插入所有其他行插入空

$model = 'MacBook Pro'; 
$lines = explode("\n", $textarea); 

foreach ($lines as $model_number) { 
     $rows_affected = $wpdb->insert($table_name, array('model'=>$model, 'model_number'=>$model_number)); 

的var_dump($ MODEL_NUMBER)返回:

string(6) "A4348 " string(6) "A3412 " string(5) "A8495" 

MySQL的行:

+----+-------------+----------------------------------+ 
          | 
          | 
| 87 | MacBook Pro | A8495       | 
+----+-------------+----------------------------------+ 

回答

1

我可以看到的唯一區別是串的長度。您可能會對數據庫有限制,只允許將5個字符插入表中。其次,可能會有一個限制,即字符串末尾的空間不足。

我會簡單地改變這樣的:

$rows_affected = $wpdb->insert($table_name, array('model'=>$model, 'model_number'=>$model_number)); 

爲了這個,看看這個工程,比這個我難倒等,看看有沒問題。

$rows_affected = $wpdb->insert($table_name, array('model'=>$model, 'model_number'=>str_replace(" ","",$model_number)));