PHPで$_GETや$_POSTを使った時のWarning: Undefined array keyの解決方法

1700 回閲覧されました
みなさんこんにちは、jonioです。
PHPで「$_POST」を使ったら「Warning: Undefined array key〜」のエラーが表示されました。
この時のコードは↓です。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require('db_connect.php'); | |
$name = $_POST['name']; | |
$password = $_POST['password']; | |
$submit = $_POST['submit']; | |
if(!empty($submit)){ | |
$pdo = db_connect(); | |
try{ | |
$sql = 'SELECT * FROM users WHERE name = :name and password = :password'; | |
$stmt = $pdo->prepare($sql); | |
$stmt->bindParam(':name', $name); | |
$stmt->bindParam(':password', $password); | |
$stmt->execute; | |
}catch(PDOException $e){ | |
echo 'エラー:'.$e->getMessage(); | |
die(); | |
} | |
if($row = $stmt->fetch(PDO::FETCH_ASSOC)){ | |
header('Location:main.php'); | |
exit; | |
}else{ | |
echo 'パスワードか名前に間違いがあります'; | |
} | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<link rel="stylesheet" href="css/style.css"> | |
<title>ログインページ</title> | |
</head> | |
<body> | |
<div class="title-area"> | |
<h1>ログインページ</h1> | |
<form action="" method="POST"> | |
<input type="text" class="input-area" name="name" placeholder="Your Name" required><br> | |
<input type="password" class="input-area" name="password" placeholder="Your Password" required><br> | |
<input type="submit" class="input-area submit" name="submit" value="ログイン"> | |
</form> | |
</div> | |
</body> | |
</html> |
エラーの原因は4行目・5行目・6行目ですが解決できたのでアウトプットとしてこの記事を残します。
原因はPHPのバージョン
今回のエラーが出た時の開発環境はMAMPでしたがPHPのバージョンは8.08でした。
PHPのバージョンが8以上になると今回のエラーが起きます。
解決方法
PHPのバージョンが7系までは例えば↓でした。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$name = $_POST['name']; | |
$password = $_POST['password']; | |
$submit = $_POST['submit']; |
8系以上では↓にします。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$name = filter_input(INPUT_POST, 'name'); | |
$password = filter_input(INPUT_POST, 'password'); | |
$submit = filter_input(INPUT_POST, 'submit'); |
name属性の値を受ける時ですが7系までの書き方「$_POST[‘name属性の値’];」を「filter_input(INPUT_POST, ‘name属性の値’);」にすればいいです。
今はPOSTですがGETの場合はPOSTをGETに変えればいいです。
一応コードを載せると↓です。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$name = filter_input(INPUT_GET, 'name'); | |
$password = filter_input(INPUT_GET, 'password'); | |
$submit = filter_input(INPUT_GET, 'submit'); |
解説が短いですがこれでエラーがなくなるはずです。