Advertisement
Guest User

Untitled

a guest
Mar 6th, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. <?php
  2.  
  3. $username = $_POST["username"];
  4. $password = $_POST["password"];
  5. $institution = $_POST["institution"];
  6.  
  7. $conn = pg_connect("database connection information"); //in reality this has been filled
  8. $result = pg_query($conn, "INSERT INTO institutions (i_id, name) VALUES (null, '$institution') RETURNING i_id");
  9. $insert_row = pg_fetch_row($result);
  10. $insti_id = $insert_row[0];
  11.  
  12. // INSTITUTION SAVED AND HAS ITS OWN ID BUT NO MEMBER OF STAFF ID
  13.  
  14. $resultTwo = pg_query($conn, "INSERT INTO staff VALUES (NULL, '$username', '$password', '$insti_id'");
  15. $insert_rowTwo = pg_fetch_row($resultTwo);
  16. $user_id = $insert_rowTwo[0];
  17. // USER SAVED WITH OWN ID AND COMPANY ID
  18. // ASSIGN AN INSTITUTION TO A STAFF MEMBER IF THE STAFF'S $company_id MATCHES THAT OF THE
  19. // INSTITUION IN QUESTION
  20. $update = pg_query($conn, "UPDATE institutions SET u_id = '$user_id' WHERE i_id = '$insti_id'");
  21.  
  22. pg_close($conn);
  23.  
  24. ?>
  25.  
  26. CREATE TABLE institutions(i_id serial, name text, u_id int);
  27. CREATE TABLE staff(user_id serial, username text, password text, i_id int);
  28.  
  29. WITH x AS (
  30. INSERT INTO staff(username, password, i_id) -- provide column list
  31. VALUES ('$username', '$password', nextval('institutions_i_id_seq'))
  32. RETURNING user_id, i_id
  33. )
  34. INSERT INTO institutions (i_id, u_id, name)
  35. SELECT x.i_id, x.user_id, '$institution'
  36. FROM x
  37. RETURNING u_id, i_id; -- if you need the values back, else you are done
  38.  
  39. staff (u_id serial PRIMARY KEY, ...)
  40. institution (i_id serial PRIMARY KEY, ...)
  41. institution_staff (i_id, u_id, ..., PRIMARY KEY(i_id, u_id)) -- implements n:m
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement