php初心者です。
住所録を作成しており、編集機能を作っています。
これまでCakePHPに頼っていたので、様々な機能を手軽に作れたのですが
初心に帰り、phpとmysqlだけで住所録を作成し、関数でfileを分けて
作ってみようと考えました。
そして、あとは編集機能を作成して完成です。
しかし、編集機能がどうもうまくできません。
エラーを表示する設定はしているのですが、エラーが出ないので、
自分なりに原因を探ったところ
update.phpでもし、値が入っていたら関数を呼び出し
それ以外だったら"Some Error"を表示するにしているので
edit.phpでPOSTした値がupdate.phpでは値を取得できていない。
という感じだと思います。
文法もあっているはずなのに
何が原因でpostされた値が取得できないのかわかりません。
お力を貸していただけないでしょうか。。。
以下がコードです。
php
1//index.php 2<?php 3require_once('select.php'); 4$results = 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 <!--入力--> 16 <section> 17 <form action="./insert.php" method="post" accept-charset="utf-8"> 18 <ul> 19 <li>名前 <input type="text" name="name"></li> 20 <li>カナ氏名 <input type="text" name="kana"></li> 21 <li>郵便番号 <input type="text" name="post_number"></li> 22 <li>住所 <input type="text" name="address"></li> 23 <li>電話番号 <input type="text" name="phone_number"></li> 24 <li>備考 <input type="text" name="note"></li> 25 </ul> 26 <p> <input type="submit" value="登録"> </p> 27 </form> 28 </section> 29 30 <!-- 一覧 --> 31 <section> 32 <table> 33 <caption>住所録一覧</caption> 34 <thead> 35 <tr> 36 <th>名前</th> 37 <th>カナ氏名</th> 38 <th>郵便番号</th> 39 <th>住所</th> 40 <th>電話番号</th> 41 <th>備考</th> 42 </tr> 43 </thead> 44 <tbody> 45 <?php foreach ($results as $result): ?> 46 <tr> 47 <td><?=$result['name'];?></td> 48 <td><?=$result['kana'];?></td> 49 <td><?=$result['post_number'];?></td> 50 <td><?=$result['address'];?></td> 51 <td><?=$result['phone_number'];?></td> 52 <td><?=$result['note'];?></td> 53 <td> 54 <form action="./edit.php/page=<?=$result['id']?>" method="post" accept-charset="utf-8"> 55 <input type="hidden" name="id" value="<?=$result['id']?>"> 56 <input type="submit" value="変更"> 57 </form> 58 </td> 59 <td> 60 <form action="./delete.php" method="post" accept-charset="utf-8"> 61 <input type="hidden" name="id" value="<?=$result['id']?>"> 62 <input type="submit" value="削除"> 63 </form> 64 </td> 65 </tr> 66 <?php endforeach; ?> 67 </tbody> 68 </table> 69 </section> 70</body> 71</html>
php
1//edit.php 2<?php 3require_once('select.php'); 4if($_POST['id']){ 5 $id = $_POST['id']; 6 $results = oneSelect(); 7}else{ 8 echo "What?"; 9 exit; 10} 11?> 12 13<!DOCTYPE html> 14<html> 15<head> 16 <meta charset ="utf-8"> 17 <title>住所録編集</title> 18</head> 19<body> 20 <section> 21 <form action="./update.php" method="post" accept-charset="utf-8"> 22 <input type="hidden" name="id" value="<?=$id?>"> 23 名前 <input type="text" name="name"> 24 <input type="submit" value="変更"> 25 </form> 26 </section> 27 <section> 28 <table> 29 <caption>住所録一覧</caption> 30 <thead> 31 <tr> 32 <th>名前</th> 33 <th>カナ氏名</th> 34 <th>郵便番号</th> 35 <th>住所</th> 36 <th>電話番号</th> 37 <th>備考</th> 38 </tr> 39 </thead> 40 <tbody> 41 <?php foreach ($results as $result) :?> 42 <tr> 43 <td><?=$result['name'];?></td> 44 <td><?=$result['kana'];?></td> 45 <td><?=$result['post_number'];?></td> 46 <td><?=$result['address'];?></td> 47 <td><?=$result['phone_number'];?></td> 48 <td><?=$result['note'];?></td> 49 </tr> 50 <?php endforeach; ?> 51 </tbody> 52 </table> 53 </section> 54</body> 55</html>
php
1//select.php 2<?php 3//全件取得する 4function select(){ 5 6 require_once('config.php'); 7 8 try{ 9 $pdo->beginTransaction(); 10 $sql="select * from info"; 11 $pre = $pdo->prepare($sql); 12 $pre->execute(); 13 $pdo->commit(); 14 return true; 15 }catch(PDOEception $e){ 16 echo "エラー :".$e->getMessage(); 17 exit; 18 } 19 $result = $pre->fetchAll(); 20 21 return $result; 22} 23 24//該当の一件取得する 25function oneSelect(){ 26 27 require_once('config.php'); 28 try{ 29 $id = $_POST['id']; 30 $pdo->beginTransaction(); 31 $sql ="select * from info where id = :id"; 32 $pre = $pdo->prepare($sql); 33 $pre->bindValue(':id',$id,PDO::PARAM_INT); 34 $pre->execute(); 35 $pdo->commit(); 36 return true; 37 }catch(PDOException $e){ 38 $pdo->rallback; 39 echo "取得できませんでした :".$e->getMessage(); 40 exit; 41 } 42 43 $result = $pre->fetchAll(); 44 return $result; 45 46} 47
php
1//update.php 2<?php 3if(!empty($_POST['name'])){ 4 5 update(); 6 header('location:edit.php'); 7 exit; 8 9}else{ 10 echo "Some Erro"; 11 exit; 12} 13 14function update(){ 15 require_once('config.php'); 16 $id = trim($_POST['id']); 17 $name = trim($_POST['name']); 18 try{ 19 $pdo->beginTransaction(); 20 $sql = "update info set name = :name where id = :id"; 21 $pre = $pdo->prepare($sql); 22 $pre->bindValue(':id',$id,PDO::PARAM_INT); 23 $pre->bindValue(':name',$name,PDO::PARAM_STR); 24 $pre->execute(); 25 $pdo->commit(); 26 return true; 27 }catch(PDOException $e){ 28 echo "登録できませんでした : ".$e->getMessage(); 29 exit; 30 } 31}
遷移は、
index.php => 該当する行の変更ボタンを押下する => edit.phpにはその行のデータだけ表示される
=>formに変更する値を入れてsubmit => update.phpでupdateする。
このようにしたいと思っています。
どこを直せば良いでしょうか。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。