PHPのSQL文についてわからないところがあります。
php
1 $result = $pdo->prepare('update words set word=?, sense=?, memo=? where id=?'); 2 $result -> execute(array(h($_POST['word']),h($_POST['sense']),h($_POST['memo']),$_POST['id']));
なんで変数($result)にわざわざ代入するのでしょうか。
意味が解りません。
php
1<?php 2function h($str) { 3 return htmlspecialchars($str); 4} 5//PDO()コンストラクタ インスタンス初期化 6$pdo = new PDO('mysql:host=localhost;dbname=takahashi1;charset=utf8', 'takahashi', 'takahashi' ); 7// もし修正ボタンが押されたら 8if ($_POST ['button4']) { 9 foreach ( $pdo->query ('select * from words order by id desc') as $row ) { 10 echo '<tr>'; 11 echo '<form action="" method = "post">'; 12 echo '<input type="hidden" name="command" value="update">'; 13 echo '<input type="hidden" name="id" value="', $row ['id'], '">'; 14// echo '<td>', $row ['id'], '</td>'; 15 echo '<td>'; 16 echo '<input type="text" name="word" size="10" value="', $row ['word'], '">'; 17 echo '</td>'; 18 echo '<td>'; 19 echo '<input type="text" name="sense" size="40" value="', $row ['sense'], '">'; 20 echo '</td>'; 21 echo '<td>'; 22 echo '<input type="text" name="memo" size="20" value="', $row ['memo'], '">'; 23 echo '</td>'; 24 echo '<td><button type="submit" style="border: 1px solid #ff44ff; color: #ee00ff; background-color: #eeccff;">更新</button></td>'; 25 echo '</form>'; 26 echo '<form action="" method="post">'; 27 echo '<input type="hidden" name="command" value="delete">'; 28 echo '<input type="hidden" name="id" value="', $row ['id'], '">'; 29 echo '<td><button type="submit" style="border: 1px solid #ff44ff; color: #ee00ff; background-color: #eeccff;">削除</button></td>'; 30 echo '</form>'; 31 echo '</tr>'; 32 echo "\n"; 33 34 } 35} 36if(isset($_POST['command']) || !$_POST){ 37 switch($_POST['command']){ 38 case 'update': 39 //PDOのインスタンスを代入した変数$pdoを指定することでPDOクラスに用意された機能が利用可能になる 40 $result = $pdo->prepare('update words set word=?, sense=?, memo=? where id=?'); 41 $result -> execute(array(h($_POST['word']),h($_POST['sense']),h($_POST['memo']),$_POST['id'])); 42 break; 43 case 'delete': 44 $result = $pdo->prepare('delete from words where id=?'); 45 $result->execute(array($_POST['id'])); 46 break; 47 case 'insert': 48 $result = $pdo->prepare('insert into words values(null,?,?,?)'); 49 $result->execute(array(h($_POST['word']),h($_POST['sense']),h($_POST['memo']))); 50 break; 51 case 'listDisplay': 52 break; 53 case 'search': 54 $vKeyword = isset($_POST['keyword'])?$_POST['keyword'] : ''; 55 $result = $pdo->prepare('select * from words where word like ? or sense like ? or memo like ?'); 56 $result->execute(array( 57 '%' . $vKeyword . '%', 58 '%' . $vKeyword . '%', 59 '%' . $vKeyword . '%' 60 ) ); 61 break; 62 } 63 if(!$_POST['command'] == 'search'){ 64 $result = $pdo->query('select * from words order by id desc'); 65 } 66 foreach ($result->fetchAll() as $row){ 67 echo '<tr>'; 68 // echo '<td width="30" height="35" align="center" width="110" height="35">', h($row['id']), '</td>'; 69 echo '<td class="font3"><b>', h($row['word']), '</b></td>'; 70 echo '<td width="150" class="font3">', h($row['sense']), '</td>'; 71 echo '<td width="150" class="font3">', h($row['memo']), '</td>'; 72 echo '</tr>'; 73 echo "\n"; 74 } 75} 76?>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。