關於課程
<?php
session_start();
$name=$address=$login=$password=”;
if (isset($_SESSION[‘customer’])) {
$name=$_SESSION[‘customer’][‘name’];
$address=$_SESSION[‘customer’][‘address’];
$login=$_SESSION[‘customer’][‘login’];
$password=$_SESSION[‘customer’][‘password’];
}
echo ‘<form action=”customeroutput.php” method=”post”>’;
echo ‘<table>’;
echo ‘<tr><td>姓名</td><td>’;
echo ‘<input type=”text” name=”name” value=”‘, $name, ‘”>’;
echo ‘</td></tr>’;
echo ‘<tr><td>地址
</td><td>’;
echo ‘<input type=”test” name=”address” value=”‘, $address, ‘”>’;
echo ‘</td></tr>’;
echo ‘<tr><td>登入ID</td><td>’;
echo ‘<input type=”text” name=”login” value=”‘, $login, ‘”>’;
echo ‘</td></tr>’;
echo ‘<tr><td>密碼</td><td>’;
echo ‘<input type=”password” name=”password” value=”‘, $password, ‘”>’;
echo ‘</td></tr>’;
echo ‘</table>’;
echo ‘<input type=”submit” value=”確定”>’;
echo ‘</form>’;
?>
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<?php
session_start();
$pdo=new PDO(‘mysql:host=localhost;dbname=shop;charset=utf8’,
‘root’, ‘12345678’);
if (isset($_SESSION[‘customer’])) {
$id=$_SESSION[‘customer’][‘id’];
$sql=$pdo->prepare(‘select * from customer where id!=? and login=?’);
$sql->execute([$id, $_REQUEST[‘login’]]);
} else {
$sql=$pdo->prepare(‘select * from customer where login=?’);
$sql->execute([$_REQUEST[‘login’]]);
}
if (empty($sql->fetchAll())) {
if (isset($_SESSION[‘customer’])) {
$sql=$pdo->prepare(‘update customer set name=?, address=?, ‘.
‘login=?, password=? where id=?’);
$sql->execute([
$_REQUEST[‘name’], $_REQUEST[‘address’],
$_REQUEST[‘login’], $_REQUEST[‘password’], $id]);
$_SESSION[‘customer’]=[
‘id’=>$id, ‘name’=>$_REQUEST[‘name’],
‘address’=>$_REQUEST[‘address’], ‘login’=>$_REQUEST[‘login’],
‘password’=>$_REQUEST[‘password’]];
echo ‘客戶資料修改完成。’;
} else {
$sql=$pdo->prepare(‘insert into customer values(null,?,?,?,?)’);
$sql->execute([
$_REQUEST[‘name’], $_REQUEST[‘address’],
$_REQUEST[‘login’], $_REQUEST[‘password’]]);
echo ‘客戶資料新增完成。’;
}
} else {
echo ‘登入ID已被使用,請重新設定。’;
}
?>