在textarea中处理额外的行

时间:2013-05-20 06:17:27

标签: php

请帮助...用户正在粘贴textarea中的值,这会导致添加新行......新行正在处理并上传到数据库但在某些列上没有值...我怎样才能消除处理textarea之前的额外行并上传到db?

这是我将textarea处理成数组的代码:

$PhoneNumber = array_map('mysql_real_escape_string', explode("\r\n", $_POST['PhoneNumber']));
$Createdate = array_map('mysql_real_escape_string', explode("\r\n", $_POST['Createdate']));
$RemedyTicketNo = array_map('mysql_real_escape_string', explode("\r\n", $_POST['PhoneNumber']));

//确定元素数量最少的值

$min_count = min(count($PhoneNumber), count($Createdate), count($RemedyTicketNo));

//创建数组以保存INSERT值

$values = array();

//创建INSERT值

for($index=0; $index<$min_count; $index++)
{
$values[] = "('{$RemedyTicketNo[$index]}','{$PhoneNumber[$index]}','{$Createdate[$index]}',
'$Category2','$Category3','$Status','$Date','$Severity','$BanType','$XiD')";   
}

//插入db

if (isset($RemedyTicketNo)) 
{
$sql="INSERT into tbl_main
(ars_no,phone_number,create_date,category_1,category_2,status,resolved_date,trouble_type_priority,ban_type,employee_id_name)
VALUES " . implode (',',$values); 
$result=mysql_query($sql);

我试图合并这段代码......但它似乎没有用......

$lines = preg_split('/\n+/', trim($_POST['textarea']));
$text = implode("\n", $lines);

这是我用以上代码修改代码时的代码:

$PhoneNumber = preg_split('mysql_real_escape_string', '/\n+/', trim("\r\n", $_POST['PhoneNumber']));
$Createdate = preg_split('mysql_real_escape_string', '/\n+/', trim("\r\n", $_POST['Createdate']));
$RemedyTicketNo = preg_split('mysql_real_escape_string', '/\n+/', trim("\r\n", $_POST['PhoneNumber']));


if (isset($RemedyTicketNo)) 
{
$sql="INSERT into tbl_main
    (ars_no,phone_number,create_date,category_1,category_2,status,resolved_date,trouble_type_priority,ban_type,employee_id_name)
VALUES " . implode ('\n',$values); 
$result=mysql_query($sql);

1 个答案:

答案 0 :(得分:0)

这已经修好了...我只是重新编辑了上面的代码,只添加了修剪,它起作用了:

$PhoneNumber = array_map('mysql_real_escape_string', explode("\r\n", trim($_POST['PhoneNumber'])));
$Createdate = array_map('mysql_real_escape_string', explode("\r\n", trim($_POST['Createdate'])));
$RemedyTicketNo = array_map('mysql_real_escape_string', explode("\r\n", trim($_POST['PhoneNumber'])));