如何从textarea到mysql获取多行数据

时间:2018-05-21 11:16:30

标签: php html mysql

我正在尝试将excel中的信息粘贴到textarea中,然后将其拆分为每个字段,但是我还要保留空白字段,因为并非所有字段都会有数据。

我粘贴的数据是13 columns (with some blank),每个都用标签分隔,然后再换行到新行。每天的总行数从

开始变化
1 - 100 EG:
Dr Pete    1234567890    anthony    woods    11/11/11    planned visit    12/12/12   dr roger    AMH    13/01/13   planned discharge    usual place of residence    "would be blank"   

OR

Dr Pete    1234567890    anthony    woods    11/11/11    planned visit    12/12/12   dr roger    AMH    "would be blank"   "would be blank"    "would be blank"    "would be blank" 

html表单:

<form name="form1" method="post" action="insert.php">
<textarea name="area"></textarea>
<input type="submit" name="submit" value="submit">
</form>

php insert.php:

$area = preg_split("[\t]", "$textarea");

print_r($area);

$sql = "INSERT INTO admissions (registered_gp, nhs_no, forename, surname, dob, admission_method, admitted_on, consultant, ward, discharge_date, 
discharge_reason, discharged_to, date_of_death)
VALUES ('$area')";

我已经走到这一步并得到以下错误“列数与第1行的值计数不匹配” - 我认为这是因为我列出了13个字段但只有1个值。我将如何为包括空白在内的每个字段提供足够的值。

我会假设我需要分离每一行数据,然后将每一列数据分开并给它自己的值名称?

2 个答案:

答案 0 :(得分:0)

您可以像这样更改代码并尝试

$var = 'Dr Pete    1234567890    anthony    woods    11/11/11    planned visit    12/12/12   dr roger    AMH    13/01/13    planned discharge    usual place of residence    "would be blank"';

        $area = explode("    ", $var);

        $registered_gp = $area[0] ;
        $nhs_no = $area[1] ;
        $forename = $area[2] ;
        $surname = $area[3] ;
        $dob = $area[4] ;
        $admission_method = $area[5] ;
        $admitted_on = $area[6] ;
        $consultant = $area[7] ;
        $ward  = $area[8] ;
        $discharge_date = $area[9] ;
        $discharge_reason = $area[10] ;
        $discharged_to = $area[11] ;
        $date_of_death = $area[12] ;


        $sql = mysql_query("INSERT INTO admissions (registered_gp, nhs_no, forename, surname, dob, admission_method, admitted_on, consultant, ward, discharge_date, 
discharge_reason, discharged_to, date_of_death)
VALUES ('$registered_gp','$nhs_no','$forename','$surname','$dob','$admission_method','$admitted_on','$consultant','$ward','$discharge_date','$discharge_reason','$discharged_to','$date_of_death')");

答案 1 :(得分:0)

在执行$ sql = ...

之前,用逗号更改标签
$area = str_replace("[\t]", "','", $area);