会員サイトの制作をしているのですが、
各ページでDB接続が多く、そのたびに毎回同じことを記述するのはちょっと...
なのでConnectionというクラスを作りました。
<?php class Connection{ protected $pdo = ""; public function __construct(){ if ($_SERVER['SERVER_NAME'] === 'localhost') { $dsn = 'mysql:dbname=test;host=localhost;charset=utf8'; $user = 'root'; $password = ''; } elseif ($_SERVER['SERVER_NAME'] === '') { $dsn = 'mysql:dbname=;host=;charset=utf8'; $user = ''; $password = ''; } else { } $option = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); $this->pdo = new PDO($dsn, $user, $password, $option); } } ?>
このConnectionクラスを継承してログインチェッククラスを作りました。
<?php class LoginCheck extends Connection{ private $error = array(); public function collation($email,$password){ $sql = "SELECT * FROM user_data WHERE email = ? "; $stmt = $this->pdo->prepare($sql); $stmt->execute([ $email, ]); $hitdata =$stmt->fetch(); if(!empty($_POST['login'])){ if(isset($email) && isset($password)) { if($email === $hitdata['email'] && password_verify($password,$hitdata['password']) ){ $_SESSION['id'] = $hitdata['id']; header('Location:mypage.php'); } else { $this->error['email'] = "パスワードかアドレスが正しくありません。".'<br /><br />'; } } } return $this->error; } } ?>
これは普通に使えているのですが
別のところでConnectionクラスを継承しようとするとうまくいかなくなりました。
そのコードが下記です。
エラー内容としては下記です。
Fatal error: Using $this when not in object context in C:\xampp\htdocs\project1\lib\class\check\RegisterHumanInput.php on line 9
<?php class RegisterHumanInput extends Connection{ static function human_input(){ $sql = "INSERT INTO user_data(email, password, name1, name2, birth_year, birth_month, birth_day, gender, first_post, last_post, pref, city, town, building, tel1, tel2, tel3) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; $stmt = $this->pdo->prepare($sql); $stmt->execute([ $_SESSION['email'], password_hash($_SESSION['pw'],PASSWORD_DEFAULT), $_SESSION['name1'], $_SESSION['name2'], $_SESSION['year'], $_SESSION['month'], $_SESSION['day'], $_SESSION['gender'], $_SESSION['first_post'], $_SESSION['last_post'], $_SESSION['pref'], $_SESSION['city'], $_SESSION['town'], $_SESSION['building'], $_SESSION['tel1'], $_SESSION['tel2'], $_SESSION['tel3'] ]); } } ?>
どうすればよいのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/07/04 14:47
2016/07/05 04:12