###実現したいこと
クラス内の名前重複チェックの"入力されたユーザ名は既に使われています"を
smartyのテンプレートに受け渡したい
###発生している問題
"入力されたユーザ名は既に使われています"が表示されない
###試したこと
$_SESSION[""] = $e->getMessage();
↓
$smarty->assign('hoge', $_SESSION[""]);
セッション変数に格納する方法しか思い浮かびませんでした
php
1<?php 2require_once('db_connection.php'); 3 4class User_registaration extends Db_Connection 5{ 6 //ユーザ情報登録 7 public function user_registaration($name, $pass) 8 { 9 $sql = "INSERT INTO ip_d_user_information(infouser_user_name, infouser_password) VALUES (?, ?)"; 10 $stmt = $this->link->prepare($sql); 11 $stmt->execute(array($name, password_hash($pass, PASSWORD_DEFAULT))); 12 } 13 14 //名前重複チェック 15 public function user_duplication($name) 16 { 17 $sql = "SELECT * FROM ip_d_user_information"; 18 //SQL文実行 19 $stmt = $this->link->query($sql); 20 try 21 { 22 while($item = $stmt->fetch()) 23 { 24 if($name == $item['infouser_user_name']) 25 { 26 throw new Exception('入力されたユーザ名は既に使われています'); 27 } 28 } 29 }catch(Exception $e) 30 { 31 $error = $e->getMessage(); 32 } 33 } 34} 35 36$username = $_POST["username"]; 37$password = $_POST["password"]; 38$password2 = $_POST["password2"]; 39 40$user = new User_registaration; 41 42$user->user_duplication($username); 43 44$user->user_registaration($username, $password);
php
1<?php 2require_once('Smarty.class.php'); 3require_once('../file_class/login_authentication.php'); 4require_once('../file_class/user_registration.php'); 5 6$smarty = new Smarty(); 7 8$smarty->template_dir = '../templates'; 9$smarty->compile_dir = '../templates_c'; 10$smarty->config_dir = '../configs/'; 11$smarty->cache_dir = '../cache/'; 12 13session_start(); 14 15// メッセージの初期化 16$errorMessage = ""; 17$signUpMessage = ""; 18$success = ""; 19$error = ""; 20 21$smarty->assign('signUpError', $error); 22 23$smarty->display('signup.tpl');
###修正後コード
php
1<?php 2require_once('db_connection.php'); 3 4class User_registaration extends Db_Connection 5{ 6 public function __construct() 7 { 8 parent::__construct(); 9 } 10 11 //名前重複チェック 12 public function user_duplication($name) 13 { 14 $sql = "SELECT * FROM ip_d_user_information"; 15 //SQL文実行 16 $stmt = $this->link->query($sql); 17 18 while($item = $stmt->fetch()) 19 { 20 if($name == $item['infouser_user_name']) 21 { 22 $error = "入力されたユーザ名は既に使われています"; 23 return $error; 24 } 25 } 26 } 27} 28 29$username = $_POST["username"]; 30 31$user = new User_registaration; 32 33$er = $user->user_duplication($username); 34
php
1<?php 2require_once('Smarty.class.php'); 3require_once('../file_class/login_authentication.php'); 4require_once('../file_class/user_registration.php'); 5 6$smarty = new Smarty(); 7 8$smarty->template_dir = '../templates'; 9$smarty->compile_dir = '../templates_c'; 10$smarty->config_dir = '../configs/'; 11$smarty->cache_dir = '../cache/'; 12 13session_start(); 14 15$smarty->assign('signUpError', $er); 16 17$smarty->display('signup.tpl');
回答1件
あなたの回答
tips
プレビュー