Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. <?php
  2. require_once('database.php');
  3. $obj = new Database;
  4. $obj->getConnection();
  5. class Member{
  6.  
  7. private $username;
  8. private $userpassword;
  9.  
  10. public function regist(){
  11.  
  12. if(isset($_POST['action'])&&$_POST['action']=='insert'){
  13. try{
  14.  
  15. $sql = "INSERT INTO userData(username,password)VALUES(:username,:password)";
  16. $stmh = $obj->prepare($sql);//the error occour here
  17. $stmh -> bindValue('username',$_POST['username'],PDO::PARAM_STR);
  18. $stmh -> bindValue('password',password_hash($_POST['userpassword'],PASSWORD_DEFAULT),PDO::PARAM_STR);
  19. $stmh -> execute();
  20.  
  21. echo "登録しました<br>";
  22. }catch(PDOExcetion $e){
  23. echo "error ".$e->getMessage();
  24. }
  25. }
  26.  
  27. }
  28.  
  29. public function login(){
  30. $sql = "SELECT * FROM userData WHERE username=:usrname";
  31. $stmh =$obj->prepare($sql);//the error occour here
  32. $stmh ->bindValue(':usrname',$_POST['username'],PDO::PARAM_STR);
  33. $stmh->execute();
  34. $row = $stmh->fetch(PDO::FETCH_ASSOC);
  35. if($row['username'] == false){
  36. echo"login fail";
  37. exit;
  38. }
  39. $hash = $row['password'];
  40. if (false === password_verify($_POST['userpassword'],$hash)){
  41. echo"login fail";
  42. exit;
  43. }
  44. }
  45. }
  46.  
  47. <?php
  48. class Database{
  49.  
  50. protected $pdo;
  51.  
  52. public function __construct()
  53. {
  54. $this->db_connect();
  55. }
  56.  
  57. private function db_connect()
  58. {
  59. $servername = "localhost";
  60. $username = "abc";
  61. $password = "abc";
  62. $dbname = "mydb";
  63. try
  64. {
  65. $this->pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  66. $this->pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  67. $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
  68. }catch(PDOException $e)
  69. {
  70. die("error connection ".$e->getMessage());
  71. }
  72. }
  73. public function getConnection()
  74. {
  75. return $this->pdo;
  76. }
  77. }
  78. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement