ココで多数の方の知恵を拝借し、DBに書き込む処理までたどり着けました。
処理の流れとしては、XMLの取込ーXMLをtable形式に直して表示ー問題が無ければDBに登録。
phpmyadminに書き込まれるのが最後の1行のみで、残りの5行分が恐らくinsert.phpでjson_decodeをした後にforeachで回す際に上書きされてるのが原因で1行しかDBに登録されていないと思われます。
これらを解決するには、取り出す際に配列[i]でひとつずつ取り出すのかと思いましたが、foreachがまさにそれ。で、解決にはなりそうにありません。配列を上書きさせないには、どう書けばいいのでしょうか?
import.php
1<head> 2 <link rel="stylesheet" href="css/import.css" type="text/css"> 3 <link href="https://fonts.googleapis.com/css?family=M+PLUS+1p&display=swap" rel="stylesheet"> 4 <title>インポート確認</title> 5 <script type="text/javascript"> 6 function submitChk () { 7 /* 確認ダイアログ表示 */ 8 var flag = confirm ("この内容で登録して宜しいですか?\n\n登録したくない場合は[キャンセル]ボタンを押して下さい"); 9 /* send_flg が TRUEなら送信、FALSEなら送信しない */ 10 return flag; 11 } 12 function set2fig(num) { 13 // 桁数が1桁だったら先頭に0を加えて2桁に調整する 14 var ret; 15 if( num < 10 ) { ret = "0" + num; } 16 else { ret = num; } 17 return ret; 18 }//時計 19 function showClock2() { 20 var nowTime = new Date(); 21 var nowHour = set2fig( nowTime.getHours() ); 22 var nowMin = set2fig( nowTime.getMinutes() ); 23 var nowSec = set2fig( nowTime.getSeconds() ); 24 var msg = nowHour + ":" + nowMin + ":" + nowSec; 25 document.getElementById("RealtimeClockArea").innerHTML = msg; 26 }//時計 27 setInterval('showClock2()',1000); 28 </script> 29</head> 30<?php 31try{ 32$pdo = new PDO('mysql:host=localhost;dbname=workinfo;charset=utf8', 'root','root', 33 array(PDO::ATTR_EMULATE_PREPARES => false)); 34} catch (PDOException $e) { 35 exit('データベース接続失敗。'.$e->getMessage()); 36} 37//工場を取得 38$location = $_REQUEST['location']; 39//理念を取得' 40$philosophy = $_REQUEST['philosophy']; 41//変数に配列を代入 42$data = array(); 43function add_person($workday, $workgroup, $personal, $product, $createcnt, $unit, $bestbydata, $remarks, $worktime, $breaktime) 44{ 45global $data; 46 47//配列にヘッダーの列名を代入 48$data []= array( 49'workday' => $workday, 50'workgroup' => $workgroup, 51'personal' => $personal, 52'product' => $product, 53'createcnt' => $createcnt, 54'unit' => $unit, 55'bestbydata' => $bestbydata, 56'remarks' => $remarks, 57'worktime' => $worktime, 58'breaktime' => $breaktime 59); 60} 61if ( $_FILES['file']['tmp_name'] ){ 62$dom = DOMDocument::load( $_FILES['file']['tmp_name'] ); 63$rows = $dom->getElementsByTagName( 'Row' ); 64//日付に項目がある場合の処理(基本) 65$workday_row = true; 66//繰り返し 67foreach ($rows as $row){ 68if ( !$workday_row ){ 69 $workday = ""; 70 $workgroup = ""; 71 $personal = ""; 72 $product = ""; 73 $createcnt = ""; 74 $unit = ""; 75 $bestbydata = ""; 76 $remarks = ""; 77 $worktime = ""; 78 $breaktime = ""; 79 80$index = 1; 81$cells = $row->getElementsByTagName( 'Cell' ); 82foreach( $cells as $cell ){ 83$ind = $cell->getAttribute( 'Index' ); 84if ( $ind != null ) $index = $ind; 85if ( $index == 1 ) $workday = $cell->nodeValue; 86if ( $index == 2 ) $workgroup = $cell->nodeValue; 87if ( $index == 3 ) $personal = $cell->nodeValue; 88if ( $index == 4 ) $product = $cell->nodeValue; 89if ( $index == 5 ) $createcnt = $cell->nodeValue; 90if ( $index == 6 ) $unit = $cell->nodeValue; 91if ( $index == 7 ) $bestbydata = $cell->nodeValue; 92if ( $index == 8 ) $remarks = $cell->nodeValue; 93if ( $index == 9 ) $worktime= $cell->nodeValue; 94if ( $index == 10) $breaktime = $cell->nodeValue; 95$index += 1; 96} 97add_person($workday, $workgroup, $personal, $product, $createcnt, $unit, $bestbydata, $remarks, $worktime, $breaktime); 98} 99$workday_row = false; 100} 101} 102//配列内の半角のスペースを全て全角に直す(エラー対策) 103array_walk_recursive($data, function (&$val, $key){ 104 $val = str_replace(' ',' ', $val); 105}); 106//json形式でinsertに渡す 107$json =json_encode($data,JSON_UNESCAPED_UNICODE); 108?> 109<html> 110<body> 111<div id="header"> 112 <img src="img/text-logo-new.png"class="titlelogo"> 113 <img src="img/logo-new.png" class="logo"> 114<h1 class="title">生産計画表 製造場所:( 115 <?php if($location == 'ogaki'){ 116 echo '大阪'; 117 }else { 118 echo '東京'; 119 } 120 ; ?>) 121</h1> 122<!--日付+曜日+時計--> 123<?php 124$week = array( "日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日" ); 125?> 126<h2>本日の日付:<?php echo date("Y/m/d").' '.'('.$week[date("w")].')';?> 127<div class="clock"> 128<h2 style="display:inline" id="RealtimeClockArea"></h2> 129</div> 130<hr> 131</div> 132<marquee><h2 class="philosophy"><?php echo $philosophy; ?></h2></marquee> 133<table border ="1" class="table"> 134<hr> 135<tr> 136<th>グループ</th> 137<th>担当者</th> 138<th>製造製品</th> 139<th>生産量</th><!--単位も含める--> 140<th>賞味期限</th> 141<th>備考</th> 142<th>就業時間</th> 143<th>休憩時間</th> 144</tr> 145<?php foreach( $data as $row ) { ?> 146<tr> 147<td><?php echo( $row['workgroup'] ); ?></td> 148<td><?php echo( $row['personal'] ); ?></td> 149<td><?php echo( $row['product'] ); ?></td> 150<td><?php echo( $row['createcnt'].$row['unit'] ); ?></td> 151<td><?php echo( $row['bestbydata'] ); ?></td> 152<td><?php echo( $row['remarks'] ); ?></td> 153<td><?php echo( $row['worktime'] ); ?></td> 154<td><?php echo( $row['breaktime'] ); ?></td> 155</tr> 156<?php } ?> 157</table> 158<form enctype="multipart/form-data" action="insert.php" method="post" onsubmit="return submitChk()"> 159<input type="hidden" name="json" value=<?php echo $json ?>> 160<input type="hidden" name="location" value=<?php echo $location ?>> 161<input type="submit" value="登録" /> 162</body> 163</html>
insert.php
1<head> 2 <link rel="stylesheet" href="css/import.css" type="text/css"> 3 <link href="https://fonts.googleapis.com/css?family=M+PLUS+1p&display=swap" rel="stylesheet"> 4 <title>インポート完了</title> 5 <script type="text/javascript"> 6 </script> 7</head> 8<?php 9$data = array(); 10function add_person($workday, $workgroup, $personal, $product, $createcnt, $unit, $bestbydata, $remarks, $worktime, $breaktime){ 11$data []= array( 12'workday' => $workday, 13'workgroup' => $workgroup, 14'personal' => $personal, 15'product' => $product, 16'createcnt' => $createcnt, 17'unit' => $unit, 18'bestbydata' => $bestbydata, 19'remarks' => $remarks, 20'worktime' => $worktime, 21'breaktime' => $breaktime 22); 23} 24try{ 25$pdo = new PDO('mysql:host=localhost;dbname=workinfo;charset=utf8', 'root','root', 26array(PDO::ATTR_EMULATE_PREPARES => false)); 27} catch (PDOException $e) { 28 exit('データベース接続失敗。'.$e->getMessage()); 29} 30//locationの取得 31$location = $_REQUEST['location']; 32//JSON形式の$data配列を取り出す 33$json= $_REQUEST['json']; 34$json = json_decode($json,true); 35 36foreach ($json as $row) { 37 $workday = $row['workday']; 38 $workgroup = $row['workgroup']; 39 $personal = $row['personal']; 40 $product = $row['product']; 41 $createcnt = $row['createcnt']; 42 $unit = $row['unit']; 43 $bestbydata = $row['bestbydata']; 44 $remarks = $row['remarks']; 45 $worktime = $row['worktime']; 46 $breaktime = $row['breaktime']; 47} 48$sql = "INSERT INTO $location (workday, workgroup, personal, product, createcnt, unit, bestbydata, remarks, worktime, breaktime) 49Values(:workday, :workgroup, :personal, :product, :createcnt, :unit, :bestbydata, :remarks, :worktime, :breaktime)"; 50$stmt = $pdo -> prepare($sql); 51$workday = substr($workday,0,10); 52//$stmt -> bindParam(':location',$location,PDO::PARAM_STR); 53$stmt -> bindParam(':workday',$workday,PDO::PARAM_STR); 54$stmt -> bindParam(':workgroup',$workgroup,PDO::PARAM_STR); 55$stmt -> bindParam(':personal',$personal,PDO::PARAM_STR); 56$stmt -> bindParam(':product',$product,PDO::PARAM_STR); 57$stmt -> bindParam(':createcnt',$createcnt,PDO::PARAM_STR); 58$stmt -> bindParam(':unit',$unit,PDO::PARAM_STR); 59$stmt -> bindParam(':bestbydata',$bestbydata,PDO::PARAM_STR); 60$stmt -> bindParam(':remarks',$remarks,PDO::PARAM_STR); 61$stmt -> bindParam(':worktime',$worktime,PDO::PARAM_STR); 62$stmt -> bindParam(':breaktime',$breaktime,PDO::PARAM_STR); 63$stmt->execute(); 64 65?> 66<table border ="1" class="table"> 67<tr> 68<th>グループ</th> 69<th>担当者</th> 70<th>製造製品</th> 71<th>生産量</th><!--単位も含める--> 72<th>賞味期限</th> 73<th>備考</th> 74<th>就業時間</th> 75<th>休憩時間</th> 76</tr> 77<?php foreach( $json as $row ) { ?> 78<tr> 79<td><?php echo( $row['workgroup'] ); ?></td> 80<td><?php echo( $row['personal'] ); ?></td> 81<td><?php echo( $row['product'] ); ?></td> 82<td><?php echo( $row['createcnt'].$row['unit'] ); ?></td> 83<td><?php echo( $row['bestbydata'] ); ?></td> 84<td><?php echo( $row['remarks'] ); ?></td> 85<td><?php echo( $row['worktime'] ); ?></td> 86<td><?php echo( $row['breaktime'] ); ?></td> 87</tr> 88<?php } ?> 89</table> 90</body> 91</html>
回答1件
あなたの回答
tips
プレビュー