如何避免从textarea插入空行或空行?

时间:2013-05-08 06:37:35

标签: php

我需要帮助删除在我的textarea中粘贴值后创建的额外行,

这段代码工作正常,但是如果在我的textarea中添加了一个空白行,它还会将其上传到我的数据库中,这会弄乱我的数据库因为它错过了主键(ars_no)条目所以我无法将其删除...我需要在上传到db之前删除额外的行,我试图使用下面的代码无济于事:

function removeEmptyLines($RemedyTicketNo)
{
return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $RemedyTicketNo);
}

这是我的INSERT到db的代码:

require 'include/DB_Open.php';

    if(isset($_POST['DBLoad']))
{
    //print_r($_POST);
    //die();
    $Category2 = $_POST['Category2'];
    $Category3 = $_POST['Category3'];
    $Status = $_POST['Status'];
    $Date = $_POST['Date'];
    $Severity = $_POST['Severity'];
    $BanType = $_POST['BanType'];
    $XiD = $_POST['XiD'];
    $Ticket = $_POST['Ticket'];

    //Process the input textareas into arrays
    $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']));

    //Determine the values with the least amoutn of elements
    $min_count = min(count($PhoneNumber), count($Createdate), count($RemedyTicketNo));

    //Create array to hold INSERT values
    $values = array();

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

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

    header("Location: smp_backend.php");
    }
}

0 个答案:

没有答案