<!DOCTYPE html>
<meta charset="utf-8" />
<?php
include 'lib_my.php';
include 'lib_arr.php';
$action = '';
if(isset($_POST['action']))$action = $_POST['action'];
//폼이 입력되었을 때 처리부분
if($action == 'form_submit') {
$table_name = trim($_POST['table_name']);
//엑셀 자료를 행으로 나누기
$rows = explode("\n", $_POST['excel_text']);
for($i=0; $i<count($rows);$i++) {
//공백만 있는 줄은 완전히 비움
if(trim($rows[$i])=='')$rows[$i] = '';
else {
//열로 나누기
$rows[$i] = explode("\t", $rows[$i]);
$rows[$i] = array_map('trim', $rows[$i]);
}
}
//빈 줄 제거
$rows = array_filter($rows);
//첫줄에서 컬럼명 추출
$colnames = $rows[0];
array_splice($rows, 0, 1);
//배열로 정리
for($i=0;$i<count($rows);$i++) {
$rows[$i] = array_combine($colnames, $rows[$i]);
}
//배열을 DB테이블로 삽입
insert_rows($rows, $table_name, 'test');
exit;
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="hidden" name="action" value="form_submit" />
<br/>테이블명: <select name="table_name">
<?php
$tabs = query_arr("SHOW TABLES");
foreach($tabs as $tab) {
echo "<option value='$tab'>$tab</option>";
}
?>
</select>
<br/>내용
<br/><textarea name="excel_text" style="width:100%;height:300px;"></textarea>
<br/><input type="submit" value="제출하기" />
</form>
실행결과
INSERT INTO table1 (emp_no,dept_name,emp_name) VALUES ('201102001','영업팀','한놈');
INSERT INTO table1 (emp_no,dept_name,emp_name) VALUES ('201201002','개발팀','두시기');
INSERT INTO table1 (emp_no,dept_name,emp_name) VALUES ('201201003','개발팀','석삼');
insert_rows($rows, $table_name);