尝试将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 |
+----+-------------+----------------------------------+
答案 0 :(得分: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)));