前提・実現したいこと
pdoを使ってdbに値をインサートしたい。
main.php->db.php
どうぞよろしくお願いします。
発生している問題・エラーメッセージ
dbの更新処理はできているが、insertの処理ができない。
該当のソースコード
php
1//main.php 2<?php 3require_once (__DIR__ . '/dbconnect.php'); 4 5//DBから対象月のデータを取得 6$pdo = ""; 7$data = database($pdo); 8function database($pdo) { 9 try { 10 $pdo = getDB(); 11 $stmt = $pdo->prepare('SELECT date, start_time, finish_time, binding_time, break_time, working_time FROM kintai'); 12 $stmt->execute(); 13 $results = $stmt->fetchAll(PDO::FETCH_ASSOC); 14 } catch (PDOException $e) { 15 echo $e->getMessage(); 16 exit; 17 } 18 return $results; 19} 20?> 21 22 23<!-- <a href="index.php">ファイルアップロード</a> --> 24 25<form method="post" action="db.php"> 26<table> 27 <?php if($data): ?> 28 <?php $j = 0;?> 29 <?php foreach ($data as $row): ?> 30 <tr> 31 <?php foreach ($row as $value => $column ): ?> 32 <td> 33 <input type="text" name="<?php echo $value.$j?>" size="10" value="<?php if($column){ echo $column; } ?>"> 34 </td> 35 <?php endforeach; ?> 36 </tr> 37 <?php $j++;?> 38 <?php endforeach; ?> 39 <?php else: ?> 40 <?php for($i=0; $i < 31; $i++): ?> 41 <tr> 42 <td> 43 <input type="text" name="date<?php echo $i;?>" size="10" value="<?php if( !empty($_POST['date']) ){ echo $_POST['date']; } ?>"> 44 <input type="text" name="start_time<?php echo $i;?>" size="10" value="<?php if( !empty($_POST['start_time']) ){ echo $_POST['start_time']; } ?>"> 45 <input type="text" name="finish_time<?php echo $i;?>" size="10" value="<?php if( !empty($_POST['finish_time']) ){ echo $_POST['finish_time']; } ?>"> 46 <input type="text" name="binding_time<?php echo $i;?>" size="10" value="<?php if( !empty($_POST['binding_time']) ){ echo $_POST['binding_time']; } ?>"> 47 <input type="text" name="break_time<?php echo $i;?>" size="10" value="<?php if( !empty($_POST['break_time']) ){ echo $_POST['break_time']; } ?>"> 48 <input type="text" name="working_time<?php echo $i;?>" size="10" value="<?php if( !empty($_POST['working_time']) ){ echo $_POST['working_time']; } ?>"> 49 </td> 50 </tr> 51 <?php endfor; ?> 52 <?php endif; ?> 53</table> 54<input type="submit" value="保存"> 55</form> 56
該当のソースコード
php
1//db.php 2<?php 3require_once (__DIR__ . '/dbconnect.php'); 4// echo '<pre>'; 5// var_dump($_POST); 6// echo '</pre>'; 7 8// insertまたはupdate処理 9$pdo = getDB(); 10 11 for($i=0; $i < count($_POST)/6; $i++) { 12 $date = $_POST['date'. $i]; 13 $start_time = $_POST['start_time'. $i]; 14 $finish_time = $_POST['finish_time'. $i]; 15 $binding_time = $_POST['binding_time'. $i]; 16 $break_time = $_POST['break_time'. $i]; 17 $working_time = $_POST['working_time'. $i]; 18 19 // if (is_null($date) && empty($date)) { 20 // continue; 21 // } 22 // if (is_null($start_time) || empty($start_time)) { 23 // continue; 24 // } 25 // if (is_null($finish_time) || empty($finish_time)) { 26 // continue; 27 // } 28 // 29 // if (is_null($binding_time) || empty($binding_time)) { 30 // continue; 31 // } 32 // 33 // if (is_null($break_time) || empty($break_time)) { 34 // continue; 35 // } 36 // 37 // if (is_null($working_time) || empty($working_time)) { 38 // continue; 39 // } 40 41 try { 42 $sql = $pdo->prepare("SELECT * FROM kintai WHERE date=:date"); 43 $sql->bindValue(':date',$date); 44 $result = $sql->execute(); 45 46 $stmt=null; 47 if ($result){ 48 $stmt = $pdo->prepare("UPDATE kintai SET start_time=:start_time,finish_time=:finish_time,binding_time=:binding_time,break_time=:break_time,working_time=:working_time WHERE date=:date"); 49 50 } else { 51 $stmt = $pdo->prepare("INSERT INTO kintai(date,start_time,finish_time,binding_time,break_time, working_time)VALUES(:date,:start_time,:finish_time,:binding_time,:break_time,:working_time)"); 52 53 } 54 55 $stmt->bindValue(':date',$date); 56 $stmt->bindValue(':start_time',$start_time); 57 $stmt->bindValue(':finish_time',$finish_time); 58 $stmt->bindValue(':binding_time',$binding_time); 59 $stmt->bindValue(':break_time',$break_time); 60 $stmt->bindValue(':working_time',$working_time); 61 62 $stmt->execute(); 63 } catch (PDOException $e) { 64 echo $e->getMessage(); 65 exit(); 66 } 67 } 68$pdo = null; 69 70//トップへ戻る 71echo '<button type="button" onclick=history.back()><a href="main.php">トップページへ戻る</a></button>'; 72
該当のソースコード
php
1<?php 2 3function getDB() { 4 $dsn = 'mysql:dbname=sc_map; host=localhost'; 5 $user = 'root'; 6 $password = ''; 7 8 try { 9 $pdo = new PDO($dsn,$user,$password); 10 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 11 $pdo->query('set names utf8'); 12 } catch (PDOException $e) { 13 echo $e->getMessage(); 14 exit(); 15 } 16 return $pdo; 17} 18
試したこと
もう一度流れを確認。main.phpからpostでdb.phpへ。データベースに値がある場合はupdateの処理、ない場合はinsertの処理。
補足情報(FW/ツールのバージョンなど)
dbconnect.phpでpdoの処理を記述。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/10 01:21