如何在while循环语句中插入许多有价值的数据。 mysql PHP

时间:2017-08-09 01:19:06

标签: php mysql

在我的PHP页面中使用了Transaction。因为hpcid是IDC表的外键。

我有一百行的人信息(名字,身份证,电话)会插入到IDC表中,所以我想使用while循环。

没有while循环,我测试了人信息的三行数据,它运行正常。但是当添加while循环时,它的工作失败。

在这里,代码可以正常工作:

$query="begin ;
    insert into HPC (name, begindata, enddate) VALUES('".$_POST['customerName']."','".$_POST['firstDay']."','".$_POST['lastDay']."');
    insert into  IDC (name,id,phone,hpcid) VALUES('".$_POST['enName3']."','".$_POST['enID3']."','".$_POST['enPhone3']."',LAST_INSERT_ID());               
    insert into  IDC (name,id,phone,hpcid) VALUES('".$_POST['enName4']."','".$_POST['enID4']."','".$_POST['enPhone4']."',LAST_INSERT_ID());
    insert into  IDC (name,id,phone,hpcid) VALUES('".$_POST['enName5']."','".$_POST['enID5']."','".$_POST['enPhone5']."',LAST_INSERT_ID());
    commit;";
$stmt=$conn->query($query);

这里,添加while循环的代码失败了:

$query="begin ;
        declare i int;
        set i = 5;
        insert into ".$_GET['table']." (name, begindate,enddate) VALUES('".$_POST['customerName']."','".$_POST['firstDay']."','".$_POST['lastDay']."');
       insert into  IDC (name,id,phone,hpcid) VALUES('".$_POST['enName3']."','".$_POST['enID3']."','".$_POST['enPhone3']."',LAST_INSERT_ID());                
       insert into  IDC (name,id,phone,hpcid) VALUES('".$_POST['enName4']."','".$_POST['enID4']."','".$_POST['enPhone4']."',LAST_INSERT_ID());
        while i<106
        do 
        insert into  IDC (name,id,phone,hpcid) VALUES('".$_POST["enName'+i+'"]."','".$_POST["enID'+i+'"]."','".$_POST["enPhone'+i+'"]."',LAST_INSERT_ID());
        set i = i +1;
        end while;
        commit;";
 $stmt=$conn->query($query);

我认为在while循环中“ename'+ i +'”是错误的。我hava尝试了enNamei,enName $ i,“enName”+ i,但所有这些都是错误的风格。

首先,我想了解如何使用while循环将多行数据插入到IDC表中。例如100行数据,如:

name     id    phone
john1     1    123
john2     2    234
john3     3    345
......
john100   100   3453

谁能帮帮我?

0 个答案:

没有答案