以下のコードですが、なぜか45行目でエラーが出ます。
{echo $err_msg;}
とするとエラーが出なくなるのですが、理由が全くわかりません。
よろしくお願いします。
index.php
1<?php 2$err_msg = ""; 3 4if(isset($_POST['login'])){ 5 $username = $_POST['username']; 6 $password = $_POST['password']; 7 try { 8 $db = new PDO('mysql:host=localhost;port=8889;dbname=sample', 'root', 'root'); 9 $sql = 'select count(*) from users where username=? and password=?'; 10 $stmt = $db->prepare($sql); 11 $stmt->execute(array($username, $password)); 12 $result = $stmt->fetch(); 13 $stmt = null; 14 $db = null; 15 16 17 if($result[0] != 0){ 18 header('Location: http://localhost:8888/bulletin/home.php'); 19 exit; 20 }else{ 21 $err_msg = "ユーザー名またはパスワードが間違っています。"; 22 } 23 24 }catch(PDOException $e){ 25 echo $e->getMessage(); 26 exit; 27 } 28} 29 30 ?> 31 32 33 34<!DOCTYPE html> 35<html lang="ja"> 36<head> 37 <meta charset="UTF-8"> 38 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 39 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 40 <title>サンプル掲示板</title> 41</head> 42<body> 43 <h1>ログイン画面</h1> 44 <form action="" method="POST"> 45 <?php if($err_msg !== null && $err_msg !== ''){echo $err_msg "<br>";} ?> 46 ユーザー名<input type="text" name="username" value=""><br> 47 パスワード<input type="password" name="username" value=""><br> 48 <input type="submit" name="login" value="ログイン"> 49 </form> 50 <a href="signin.php">新規登録</a> 51</body> 52</html>

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。