Guest User

Untitled

a guest
Nov 25th, 2017
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. user_id (primary, AI)
  2. username (unique)
  3. email (unique)
  4. password
  5. field 1
  6. field 2
  7. ...
  8.  
  9. LOCK TABLES users WRITE;
  10. // users provided username and email are already taken?
  11. SELECT COUNT(username) username, (SELECT COUNT(email) FROM users WHERE email = 'batman@heroes.com') email FROM users WHERE username = 'batman'
  12.  
  13. $res = fetch_array;
  14. if($res['username'] == 0 && $res['email'] == 0){
  15. INSERT INTO users (username,email,password) VALUES ('batman','batman@heroes.com','imtheonlytruehero')
  16.  
  17. if(affected_rows == 1)
  18. $username = null;
  19. $email= null;
  20. $registration = true;
  21. }
  22. else {
  23. $username = null;
  24. $email= null;
  25. $registration = false;
  26. }
  27. }
  28. else {
  29. $username = ($res['username'] > 0) ? false : true;
  30. $email = ($res['email'] > 0) ? false : true;
  31. $registration = false;
  32. }
  33. UNLOCK TABLES;
  34.  
  35. START TRANSACTION;
  36. INSERT INTO users (username,email,password) VALUES ('batman','0','imtheonlytruehero');
  37.  
  38. if(affected_rows > 0) {
  39. $username = true;
  40. UPDATE users SET email='batman@heroes.com' WHERE username='batman';
  41. if(affected_rows > 0) {
  42. $email = true;
  43. $registration = true;
  44. COMMIT;
  45. }
  46. else {
  47. $email = false;
  48. $registration = true;
  49. ROLLBACK;
  50. }
  51. }
  52. else {
  53. $username = false;
  54. $email = null;
  55. $registration = false;
  56. ROLLBACK;
  57. }
  58.  
  59. while (1) {
  60. START TRANSACTION;
  61. SELECT * FROM users WHERE username = ? OR email = ?;
  62.  
  63. if (something_found) {
  64. // figure out what was found — email, login or both
  65. // ...
  66. } else {
  67. try {
  68. INSERT INTO users …;
  69. } catch (UniqueViolation) {
  70. // somebody else could have inserted a record with the same
  71. // username/email after our SELECT query
  72. ROLLBACK;
  73. // try again
  74. continue;
  75. }
  76. }
  77. COMMIT;
  78. break;
  79. }
  80.  
  81. $result = $db->query('SELECT COUNT(id) as total FROM USER WHERE username = "batman" or email = "batman@heroes.com"');
  82. $row = $result->fetch_assoc();
  83. if($row['total'] > 0}{
  84. // redirect back to form, exit here
  85. }
  86.  
  87. try{
  88. // no user with email, username exists, so try to insert that user
  89. $db->query('INSERT INTO USER ...');
  90. $registration = true;
  91. }
  92. catch(Exception e){
  93. // If a different user with same username or email registered in
  94. // the mean time then redirect to form.
  95. }
Add Comment
Please, Sign In to add comment