load_user_canvases.php 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. /* Retrieves all the canvases created by the user passed by parameter in JSON format */
  3. // Retrieve user credentials
  4. $email = $_POST['loggedin_user_email'];
  5. require_once('../../php/db_utils.php');
  6. $conn = db_connect(); // Connect to the database
  7. // Check if the username already exists
  8. if(!($result = mysqli_query($conn, 'SELECT name FROM user WHERE username = "' . $email . '";'))) {
  9. echo 400; // Wrong query
  10. }
  11. else if(mysqli_num_rows($result) != 1) { // User not registered or duplicated
  12. echo 401;
  13. }
  14. else { // User registered: Retrieve canvases
  15. if(!($result = mysqli_query($conn, 'SELECT * FROM canvas WHERE user_id = "' . $email . '" ORDER BY canvas_name;'))) {
  16. echo 400; // Wrong query
  17. }
  18. else { // Request accepted
  19. $rows = array();
  20. while($r = mysqli_fetch_assoc($result)) {
  21. $rows[] = $r;
  22. }
  23. echo json_encode($rows);
  24. }
  25. }
  26. mysqli_free_result($result);
  27. db_close($conn); // Close the database
  28. ?>