Skip to content

Commit

Permalink
chapter6-5
Browse files Browse the repository at this point in the history
  • Loading branch information
yuukinoda committed May 8, 2022
1 parent ce97e92 commit 6c36969
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
/member_picture
10 changes: 10 additions & 0 deletions dbconnect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

try {
$db = new PDO('mysql:dbname=mini_bbs;host = 127.0.0.1; charset=utf8', 'root', '');
} catch (PDOException $e) {
echo 'DB接続エラー:' . $e->getMessage();
}

?>

42 changes: 39 additions & 3 deletions join/check.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,49 @@
<?php
ini_set("display_errors", "Off");
session_start();
require('../dbconnect.php');

if (!isset($_SESSION['join'])) {
header('Location:index.php');
exit();
}

if (!empty($_POST)) {
//登録処理
$statement = $db->prepare('INSERT INTO members SET name=?,email=?,password=?, picture=?, created=NOW()');

echo $ret = $statement->execute(array(
$_SESSION['join']['name'],
$_SESSION['join']['email'],
sha1($_SESSION['join']['password']),
$_SESSION['join']['image']
));
unset($_SESSION['join']);

header('Location:thanks.php');
exit();
}

?>

<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit"/>
<dl>
<dt>ニックネーム</dt>
<dd></dd>
<dd>
<?php echo htmlspecialchars($_SESSION['join']['name'], ENT_QUOTES); ?>
</dd>
<dt>メールアドレス</dt>
<dd></dd>
<dd>
<?php echo htmlspecialchars($_SESSION['join']['email'], ENT_QUOTES); ?>
</dd>
<dt>パスワード<span class="required">必須</dt>
<dd>【開示されません】</dd>
<dt>写真など</dt>
<dd></dd>
<dd>
<img src="../member_picture/<?php echo htmlspecialchars($_SESSION['join']['image'], ENT_QUOTES); ?>"
width="100" height="100" alt="">
</dd>
</dl>
<div>
<a href="index.php?action=rewrite">&laquo;&nbsp;書き直す</a>|
Expand Down
35 changes: 27 additions & 8 deletions join/index.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

ini_set("display_errors", "Off");
session_start();
require('../dbconnect.php');

if(!empty($_POST)) {
if (!empty($_POST)) {
//エラー項目の確認
if ($_POST['name'] == '') {
$error['name'] = 'blank';
Expand All @@ -22,19 +22,28 @@
}

$fileName = $_FILES['image']['name'];
if(!empty($fileName)){
$ext = substr($fileName,-3);
if($ext != 'jpg' && $ext!='gif'){
$error['image']='type';
if (!empty($fileName)) {
$ext = substr($fileName, -3);
if ($ext != 'jpg' && $ext != 'gif') {
$error['image'] = 'type';
}
}

//重複アカウントのチェック
if (empty($error)) {
$member = $db->prepare('SELECT COUNT(*) AS cnt FROM members WHERE email=?');
$member->execute(array($_POST['email']));
$record = $member->fetch();
if ($record['cnt'] > 0) {
$error['email'] = 'duplicate';
}
}


if (empty($error)) {
//画像をアップロードする
$image = date('YmdHis').$_FILES['image']['name'];
move_uploaded_file($_FILES['image']['tmp_name'],'../member_picture/'.$image);
$image = date('YmdHis') . $_FILES['image']['name'];
move_uploaded_file($_FILES['image']['tmp_name'], '../member_picture/' . $image);

$_SESSION['join'] = $_POST;
$_SESSION['join']['image'] = $image;
Expand All @@ -43,6 +52,13 @@
}
}

//書き直し

if ($_REQUEST['action'] == 'rewrite') {
$_POST = $_SESSION['join'];
$error['rewrite'] = true;
}

?>

<p>次のフォームに必要事項をご記入ください。</p>
Expand All @@ -62,6 +78,9 @@
<?php if ($error['email'] == 'blank'): ?>
<p class="error">*メールアドレスを入力してください</p>
<?php endif; ?>
<?php if ($error['email'] == 'duplicate'): ?>
<p class="error">*指定されたメールアドレスはすでに登録されています</p>
<?php endif; ?>
<dt>パスワード<span class="required">必須</dt>
<dd><input type="password" name="password" size="10" maxlength="20"
 value="<?php echo htmlspecialchars($_POST['password'], ENT_QUOTES); ?>"/></dd>
Expand Down

0 comments on commit 6c36969

Please sign in to comment.