どうも!Twitterの詩人でお馴染みのざんをですっ!
「PHPの画像処理ってなんであんなにややこしいんだろなぁ。ぴえん。。」
って思っているそこのあなた!
安心してください。履いてますよ!!
じゃなくて、、、今回はそんな不安を解消すべく私ざんをが一言コメントと画像処理を綺麗に場合分けして一回の更新処理で済ませる方法を紹介していきます笑
info.php
<?php
session_start();
~
$id = $_GET['user_id'];
$db_user = "xxxxxx";
$db_pass = "xxxxxx";
$dsn = "xxxxxx";
try {
$dbh = new PDO($dsn, $db_user, $db_pass);
} catch (PDOException $e) {
echo $e->getmessage();
exit;
}
$sql = "SELECT * FROM members WHERE id = :id";
$stmt = $dbh->prepare($sql);
$stmt->execute(array(':id' => $id));
$value = $stmt->fetch();
$dbh = null;
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
</head>
<body>
<h1>ユーザー情報</h1>
〜
<p>画像:
<?php if ($value['image'] === ''): ?>
<p>未登録<p>
<?php else: ?>
<p><?php echo '<img src="./image/' . $value['image'] . '" width="190" height="145">'; ?></p>
<?php endif; ?>
<p>一言コメント:<?php echo $value['comment']; ?></p>
<!-- ログイン者であり、そのログインユーザーの情報のみ編集リンクが現れる -->
<?php if (isset($_SESSION['id']) && $_SESSION['id'] == $id): ?>
<!-- aタグのgetでよるIDと一言コメントと画像の名前を送信 -->>
<a href="info_edit.php?id=<?php echo $id; ?>&comment=<?php echo $value['comment']; ?>&image=<?php echo $value['image']; ?>">編集</a><br>
<?php endif; ?>
〜
</body>
</html>
とユーザーのそれぞれの詳細表示画面がこれになります。
続いて編集画面
info_edit.php
<?php
session_start();
if (!isset($_SESSION['id']) || !isset($_SESSION['name'])) {
header('Location: index.php');
exit();
}
$id = $_GET['id'];
$comment = $_GET['comment'];
$image = $_GET['image'];
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
</head>
<body>
<h1>ユーザー情報編集画面</h1>
<form method="post" enctype="multipart/form-data" action="info_comp.php?id=<?php echo $id; ?>&image=<?php echo $image; ?>">
<p>画像</p>
<input type="file" name="image">
<p>一言コメント</p>
<textarea name="comment"><?php echo $comment; ?></textarea><br>
<input type="submit" value="変更"><br>
<a href="info.php?id=<?php echo $id; ?>">戻る</a>
</form>
</body>
</html>
formはpostだけれども、こうすることによってget送信も同時に出来るんです!
なんでかっていうのは次の処理の時にわざわざ画像データ検索する処理を減らすようにこうやって使いまわしているだけです。
続いては編集の裏側の処理です!
info_comp.php
<?php
session_start();
// URLの直接入力を弾く
if (!isset($_GET['id']) || !isset($_FILES['image']['name']) || !isset($_POST['comment'])) {
header('Location: index.php');
exit;
}
$id = $_GET['id'];
$comment = $_POST['comment'];
$user = "xxxxxx";
$pass = "xxxxxx";
$dsn = "xxxxxx";
try {
$dbh = new PDO($dsn, $user, $pass);
} catch (PDOException $e) {
echo $e->getmessage();
exit;
}
// 他のユーザーが変更できないようにする
if ($_SESSION['id'] == $id) {
// 画像の形式チェック
$type = $_FILES['image']['type'];
if ($_FILES['image']['error'] || $type === 'image/jpeg' || $type === 'image/png' || $type === 'image/gif') {
// 画像が変更無しの場合の処理
if ($_FILES['image']['error']) {
$image = $_GET['image'];
} else {
// ランダムな文字列(かぶる心配のないものならなんでもいい)
$image = mt_rand(0, 9999999999);
move_uploaded_file($_FILES['image']['tmp_name'], './image/' . $image);
// データベースに画像がある場合
if ($_GET['image'] !== '') {
unlink('./image/' . $_GET['image']);
}
}
//コメントがなかった場合はnullを保存
if ($comment == '') {
$comment = null;
}
$sql = "UPDATE members SET image = :image, comment = :comment WHERE id = :id";
$stmt = $dbh->prepare($sql);
$data = array(':image' => $image, ':comment' => $comment, ':id' => $id);
$stmt->execute($data);
$msg = '変更されました';
} else {
$msg = '画像のファイル形式が正しくありません。';
}
} else {
$msg = 'このプロフィールは編集できません。';
}
$dbh = null;
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
</head>
<body>
<p><?php echo $msg; ?></p>
<a href="info.php?id=<?php echo $id; ?>">戻る</a>
</body>
</html>
これで最低限の処理でupdateできたかなって感じです笑
「これが無駄だなぁ」とか「これが足りてねえなあ」とかあれば教えていただけると幸いです!!