關於課程
<table>
<tr><th>商品編號</th><th>商品名稱</th><th>商品價格</th></tr>
<?php
$pdo=new PDO(
‘mysql:host=localhost;dbname=shop;charset=utf8’, ‘root’, ‘12345678’
);
foreach ($pdo->query(‘select * from product’) as $row) {
echo ‘<tr><form action=”updateoutput.php” method=”post”>’;
echo ‘<input type=”hidden” name=”id” value=”‘, $row[‘id’], ‘”>’;
echo ‘<td>’, $row[‘id’], ‘</td>’;
echo ‘<td>’;
echo ‘<input type=”text” name=”name” value=”‘, $row[‘name’], ‘”>’;
echo ‘</td>’;
echo ‘<td>’;
echo ‘<input type=”text” name=”price” value=”‘, $row[‘price’], ‘”>’;
echo ‘</td>’;
echo ‘<td><input type=”submit” value=”確定修改”></td>’;
echo ‘</form></tr>’;
echo “n”;
}
?>
</table>
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<?php
$pdo=new PDO(‘mysql:host=localhost;dbname=shop;charset=utf8’,
‘root’, ‘12345678’);
$sql=$pdo->prepare(‘update product set name=?, price=? where id=?’);
if (empty($_REQUEST[‘name’])) {
echo ‘請輸入商品名稱。’;
} else
if (!preg_match(‘/[0-9]+/’, $_REQUEST[‘price’])) {
echo ‘請以整數輸入商品價格。’;
} else
if ($sql->execute(
[htmlspecialchars($_REQUEST[‘name’]),
$_REQUEST[‘price’], $_REQUEST[‘id’]]
)) {
echo ‘修改成功。’;
} else {
echo ‘修改失敗。’;
}
?>