edit3.phpの
php
1 case 'insert' : 2 if (empty ( $_REQUEST ['name'] ) || ! preg_match ( '/[0-9]+/', $_REQUEST ['price'] )) 3 break; 4 $sql = $pdo->prepare ( 'insert into product values(null,?,?)' ); 5 $sql->execute ( array ( 6 htmlspecialchars ( $_REQUEST ['name'] ), 7 $_REQUEST ['price'] 8 ) );
この部分の
if(empty($_REQUEST['name']) || !preg_match('/[0-9]+/', $_REQUEST['price']))
break;
このbreakって、もし$_REQUEST['name']の値が空または$_REQUEST['price']の値が数字じゃなかったら処理を中断して、
$sql = $pdo->prepare ( 'insert into product values(null,?,?)' );
$sql->execute ( array (
htmlspecialchars ( $_REQUEST ['name'] ),
$_REQUEST ['price']
) );
は通らないという意味でしょうか。
if(empty($_REQUEST['name']) || !preg_match('/[0-9]+/', $_REQUEST['price']))以外だったら
$sql = $pdo->prepare('insert into product values(null,?,?)' );
$sql = execute(array(htmlspecialchars($_REQUEST['name']),$_REQUEST['price']));
を実行するという意味でしょうか。
php:edit3.php
1<?php require 'header.php'; ?> 2<table> 3 <tr> 4 <th>商品番号</th> 5 <th>商品名</th> 6 <th>商品価格</th> 7 </tr> 8<?php 9$pdo = new PDO ( 'mysql:host=localhost;dbname=shop;charset=utf8', 'staff', 'password' ); 10if (isset ( $_REQUEST ['command'] )) { 11 switch ($_REQUEST ['command']) { 12 case 'insert' : 13 if (empty ( $_REQUEST ['name'] ) || ! preg_match ( '/[0-9]+/', $_REQUEST ['price'] )) 14 break; 15 $sql = $pdo->prepare ( 'insert into product values(null,?,?)' ); 16 $sql->execute ( array ( 17 htmlspecialchars ( $_REQUEST ['name'] ), 18 $_REQUEST ['price'] 19 ) ); 20 break; 21 case 'update' : 22 if (empty ( $_REQUEST ['name'] ) || ! preg_match ( '/[0-9]+/', $_REQUEST ['price'] )) 23 break; 24 $sql = $pdo->prepare ( 'update product set name=?, price=? where id=?' ); 25 $sql->execute ( array ( 26 htmlspecialchars ( $_REQUEST ['name'] ), 27 $_REQUEST ['price'], 28 $_REQUEST ['id'] 29 ) ); 30 break; 31 case 'delete' : 32 $sql = $pdo->prepare ( 'delete from product where id=?' ); 33 $sql->execute ( array ( 34 $_REQUEST ['id'] 35 ) ); 36 break; 37 } 38} 39foreach ( $pdo->query ( 'select * from product' ) as $row ) { 40 echo '<tr>'; 41 echo '<form action="edit3.php" method="post">'; 42 echo '<input type="hidden" name="command" value="update">'; 43 echo '<input type="hidden" name="id" value="', $row ['id'], '">'; 44 echo '<td>', $row ['id'], '</td>'; 45 echo '<td>'; 46 echo '<input type="text" name="name" value="', $row ['name'], '">'; 47 echo '</td>'; 48 echo '<td>'; 49 echo '<input type="text" name="price" value="', $row ['price'], '">'; 50 echo '</td>'; 51 echo '<td><input type="submit" value="更新"></td>'; 52 echo '</form>'; 53 echo '<form action="edit3.php" method="post">'; 54 echo '<input type="hidden" name="command" value="delete">'; 55 echo '<input type="hidden" name="id" value="', $row ['id'], '">'; 56 echo '<td><input type="submit" value="削除"></td>'; 57 echo '</form>'; 58 echo '</tr>'; 59 echo "\n"; 60} 61?> 62<tr> 63 <form action="edit3.php" method="post"> 64 <input type="hidden" name="command" value="insert"> 65 <td></td> 66 <td><input type="text" name="name"></td> 67 <td><input type="text" name="price"></td> 68 <td><input type="submit" value="追加"></td> 69 </form> 70 </tr> 71</table> 72<?php require 'footer.php'; ?>
php:edit2.php
1<?php require 'header.php'; ?> 2<table> 3<tr><th>商品番号</th><th>商品名</th><th>商品価格</th></tr> 4<?php 5$pdo=new PDO('mysql:host=localhost;dbname=shop;charset=utf8', 6 'staff', 'password'); 7foreach ($pdo->query('select * from product') as $row) { 8 echo '<tr>'; 9 echo '<form action="edit3.php" method="post">'; 10 echo '<input type="hidden" name="command" value="update">'; 11 echo '<input type="hidden" name="id" value="', $row['id'], '">'; 12 echo '<td>', $row['id'], '</td>'; 13 echo '<td>'; 14 echo '<input type="text" name="name" value="', $row['name'], '">'; 15 echo '</td>'; 16 echo '<td>'; 17 echo '<input type="text" name="price" value="', $row['price'], '">'; 18 echo '</td>'; 19 echo '<td><input type="submit" value="更新"></td>'; 20 echo '</form>'; 21 echo '<form action="edit3.php" method="post">'; 22 echo '<input type="hidden" name="command" value="delete">'; 23 echo '<input type="hidden" name="id" value="', $row['id'], '">'; 24 echo '<td><input type="submit" value="削除"></td>'; 25 echo '</form>'; 26 echo '</tr>'; 27 echo "\n"; 28} 29?> 30<tr> 31<form action="edit3.php" method="post"> 32<input type="hidden" name="command" value="insert"> 33<td></td> 34<td><input type="text" name="name"></td> 35<td><input type="text" name="price"></td> 36<td><input type="submit" value="追加"></td> 37</form> 38</tr> 39</table> 40<?php require 'footer.php'; ?>
php:edit.php"
1<?php require 'header.php'; ?> 2<table> 3<tr><th>商品番号</th><th>商品名</th><th>商品価格</th></tr> 4<tr> 5<form action="edit3.php" method="post"> 6<input type="hidden" name="command" value="insert"> 7<td></td> 8<td><input type="text" name="name"></td> 9<td><input type="text" name="price"></td> 10<td><input type="submit" value="追加"></td> 11</form> 12</table> 13<?php require 'footer.php'; ?>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/02/20 06:00
2017/02/20 06:06
2017/02/20 06:48
2017/02/20 06:56
2017/02/20 07:18
2017/02/20 07:32
2017/02/20 07:37
2017/02/20 07:46
2017/02/20 07:59
2017/02/20 08:03
2017/02/20 08:05
2017/02/20 08:09
2017/02/20 08:24
2017/02/20 08:33
2017/02/20 08:33