PHP,Mysql,PDOで住所録を作っています。
insert,delete,selectの関数を作ってあとはupdateで完成です。
しかしフィールドが以下の様になっているのですが
1つ1つにsqlのupdateを指定するのは面倒だし簡潔にしたいので
一気に変更したいものだけupdateできる様にしたいです。
id
name
kana
post_number
address
phone_number
note
register_time
update_time
仕事でphpを使っているのですが、独自フレームワークを使っているため、
まだまだ初心者ですが、改めて一度初心に帰り、phpとmysqlだけで住所録を作って見ようと思いました。
仕事での独自フレイムワークによるupdateは、下記のような感じにしたら処理ができます。
php
1$model = new Model; 2$hash = array( 3 "field名" => 値, 4 "field名" => 値, 5 "field名" => 値 6 ); 7$model->update($hash,テーブル名);
この場合fieldが何個あっても、$hashの配列でupdateしたいものだけ入れれば
その部分だけupdateされるので、とても楽に実装できます。
以下が現在の住所録のコードです。
php
1//index.php 2<?php 3require_once('select.php'); 4$all = select(); 5?> 6 7<!DOCTYPE html> 8<html> 9<head> 10 <meta charset="utf-8"> 11 <title>住所録</title> 12</head> 13<body> 14 <h1>住所録</h1> 15 <!-- insert --> 16 <section> 17 <form action="insert.php" method="post" accept-charset="utf-8"> 18 名前<input type="text" name="name"><br> 19 カナ<input type="text" name="kana"><br> 20 郵便番号<input type="text" name="post_number"><br> 21 住所<input type="text" name="address"><br> 22 電話番号<input type="text" name="phone_number"><br> 23 備考<input type="text" name="note"><br> 24 <input type="submit" value="登録"> 25 </form> 26 </section> 27 28 <!-- select --> 29 <section> 30 <table> 31 <caption>住所</caption> 32 <thead> 33 <tr> 34 <th>名前</th> 35 <th>カナ</th> 36 <th>郵便番号</th> 37 <th>住所</th> 38 <th>電話番号</th> 39 <th>備考</th> 40 </tr> 41 </thead> 42 <tbody> 43 <?php foreach ($all as $a):?> 44 <tr> 45 <td><?=$a['name']?></td> 46 <td><?=$a['kana']?></td> 47 <td><?=$a['post_number']?></td> 48 <td><?=$a['address']?></td> 49 <td><?=$a['phone_number']?></td> 50 <td><?=$a['note']?></td> 51 <td> 52 <form action="edit.php" method="post" accept-charset="utf-8"> 53 <input type="hidden" name="id" value="<?=$a['id']?>"> 54 <input type="submit" value="変更"> 55 </form> 56 </td> 57 <td> 58 <form action="delete.php" method="post" accept-charset="utf-8"> 59 <input type="hidden" name="id" value="<?=$a['id']?>"> 60 <input type="submit" value="削除"> 61 </form> 62 </td> 63 </tr> 64 <?php endforeach; ?> 65 </tbody> 66 </table> 67 </section> 68 69</body> 70</html>
php
1//select.php 2<?php 3 4//全件取得 5function select(){ 6 require_once('config.php'); 7 try{ 8 $pdo->beginTransaction(); 9 $sql = "select * from info"; 10 $pre = $pdo->prepare($sql); 11 $pre->execute(); 12 $pdo->commit(); 13 }catch(PDOException $e){ 14 echo "selectできませんでした。: ".$e->getMessage(); 15 } 16 17 $result = $pre->fetchAll(); 18 19 return $result; 20} 21 22 23//該当の一件だけ取得 24function selectOne($id){ 25 require_once('config.php'); 26 try{ 27 $pdo->beginTransaction(); 28 $sql = "select * from info where id = :id"; 29 $pre = $pdo->prepare($sql); 30 $pre->bindValue(":id",$id,PDO::PARAM_INT); 31 $pre->execute(); 32 $pdo->commit(); 33 }catch(PDOException $e){ 34 "selectOneできませんでした。 : ". $e->getMessage(); 35 exit; 36 } 37 38 $result = $pre->fetch(); 39 return $result; 40 41}
php
1//insert.php 2<?php 3if(!empty($_POST['name'])){ 4 $name = $_POST['name']; 5 $kana = $_POST['kana']; 6 $post_number = $_POST['post_number']; 7 $address = $_POST['address']; 8 $phone_number = $_POST['phone_number']; 9 $note = $_POST['note']; 10 insert($name,$kana,$post_number,$address,$phone_number,$note); 11 header("Location:./index.php"); 12 exit; 13} 14 15//insert 16function insert($name,$kana,$post_number,$address,$phone_number,$note) 17{ 18 require_once('config.php'); 19 try{ 20 $pdo->beginTransaction(); 21 $sql = "insert into info(name,kana,post_number,address,phone_number,note,register_time,update_time) values (:name,:kana,:post_number,:address,:phone_number,:note,now(),now())"; 22 $pre = $pdo->prepare($sql); 23 $pre->bindValue(":name",$name,PDO::PARAM_STR); 24 $pre->bindValue(":kana",$kana,PDO::PARAM_STR); 25 $pre->bindValue(":post_number",$post_number,PDO::PARAM_INT); 26 $pre->bindValue(":address",$address,PDO::PARAM_STR); 27 $pre->bindValue(":phone_number",$phone_number,PDO::PARAM_STR); 28 $pre->bindValue(":note",$note,PDO::PARAM_STR); 29 $pre->execute(); 30 $pdo->commit(); 31 }catch(PDOException $e){ 32 echo "insertできませんでした。 : ".$e->getMessage(); 33 exit; 34 } 35}
php
1//edit.php 2<?php 3require_once('select.php'); 4if(!empty($_POST['id'])){ 5 $id = $_POST['id']; 6 $one = selectOne($id); 7}else{ 8 echo "取得できませんでした。"; 9} 10?> 11 12<!DOCTYPE html> 13<html> 14<head> 15 <meta charset="utf-8"> 16 <title>変更</title> 17</head> 18<body> 19 <!-- 該当住所一覧 --> 20 <section> 21 <table> 22 <caption><?=$one['name']?>さんの住所</caption> 23 <thead> 24 <tr> 25 <th>名前</th> 26 <th>カナ</th> 27 <th>郵便番号</th> 28 <th>住所</th> 29 <th>電話番号</th> 30 <th>備考</th> 31 </tr> 32 </thead> 33 <tbody> 34 <tr> 35 <td><?=$one['name']?></td> 36 <td><?=$one['kana']?></td> 37 <td><?=$one['post_number']?></td> 38 <td><?=$one['address']?></td> 39 <td><?=$one['phone_number']?></td> 40 <td><?=$one['note']?></td> 41 </tr> 42 </tbody> 43 </table> 44 </section> 45 <!-- update form --> 46 <section> 47 <form action="update.php" method="post" accept-charset="utf-8"> 48 名前<input type="text" name="name"><br> 49 カナ<input type="text" name="kana"><br> 50 郵便番号<input type="text" name="post_number"><br> 51 住所<input type="text" name="address"><br> 52 電話番号<input type="text" name="phone_number"><br> 53 備考<input type="text" name="note"><br> 54 <input type="hidden" name="id" value="<?php echo $id;?>"> 55 <input type="submit" value="変更"> 56 </form> 57 </section> 58</body> 59</html>
php
1//delete.php 2<?php 3if(!empty($_POST['id'])){ 4 $id = $_POST['id']; 5 delete($id); 6 header("Location:./index.php"); 7 exit; 8} 9function delete($id) 10{ 11 require_once('config.php'); 12 try{ 13 $pdo->beginTransaction(); 14 $sql = "delete from info where id = :id"; 15 $pre = $pdo->prepare($sql); 16 $pre->bindValue(":id",$id,PDO::PARAM_INT); 17 $pre->execute(); 18 $pdo->commit(); 19 }catch(PDOException $e){ 20 echo "deleteできませんでした。: ".$e->getMessage(); 21 exit; 22 } 23}
php
1//update.php 2<?php 3if(!empty($_POST['id']) && !empty($_POST['name'])){ 4 $id = $_POST['id']; 5 $name = $_POST['name']; 6 update($id,$name); 7 header("Location:./index.php"); 8 exit; 9}else{ 10 header("Location:./index.php"); 11} 12function update($id,$name) 13{ 14 require_once('config.php'); 15 try{ 16 $pdo->beginTransaction(); 17 $sql = "update set name = :name where id = :id"; 18 $pre = $pdo->prepare($sql); 19 $pre->bindValue(":id",$id,PDO::PARAM_INT); 20 $pre->bindValue(":name",$name,PDO::PARAM_STR); 21 $pre->execute(); 22 $pdo->commit(); 23 }catch(PDOException $e){ 24 echo "updateできませんでした。: ".$e->getMessage(); 25 exit; 26 } 27}
今のupdate.phpはnameだけ変更できる様にしています。
しかし、かなり面倒です。
どうしたら良いでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/06/24 12:18
2018/06/24 12:31
退会済みユーザー
2018/06/24 13:00