Advertisement
Guest User

Rogers

a guest
Feb 25th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.27 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: TheDigitalAcademy
  5.  * Date: 16/02/24
  6.  * Time: 12:59 PM
  7.  */
  8.  
  9. $sever = 'whm.empirestate.co.za';
  10. $user = 'empirest_applica';
  11. $pass = 'iloveempirestate';
  12. $dbName = 'da_app_db';
  13.  
  14. try {
  15.     $db = new PDO("mysql:host=whm.empirestate.co.za;dbname=da_app_db", "empirest_applica", "iloveempirestate");
  16. } catch (Exception $e) {
  17.     die("Error : " . $e->getMessage());
  18. }
  19.  
  20.  
  21.  
  22.  
  23. if (!empty($_POST)) {
  24.     if (isset($_POST['name'])) {
  25.         $username = $_POST['name'];
  26.     }
  27.  
  28.  
  29.     if (isset($_POST['email'])) {
  30.         $email = $_POST['email'];
  31.     }
  32.  
  33.     if (empty($username) || empty($pass) ) {
  34.         // Create some data that will be the JSON response
  35.         $response["success"] = 0;
  36.         $response["message"] = "Please fill in all fields.";
  37.  
  38.         //die will kill the page and not execute any code below, it will also
  39.         //display the parameter... in this case the JSON data our Android
  40.         //app will parse
  41.         die(json_encode($response));
  42.     }
  43.  
  44.     $query_params = array(
  45.         ':user' => $_POST['username']
  46.     );
  47.  
  48.     $query = "INSERT INTO Intern_tbl (Name, Personal_email)
  49.   VALUES
  50.   ('$username', '$email')";
  51.  
  52.     try {
  53.         $stmnt = $db->prepare($query);
  54.         $result = $stmnt->execute($query_params);
  55.     } catch (Exception $e) {
  56.         // For testing, you could use a die and message.
  57.         die("Failed to run query: " . $ex->getMessage());
  58.  
  59.         //or just use this use this one:
  60.         //$response["success"] = 0;
  61.         //$response["message"] = "Database Error2. Please Try Again!";
  62.         //die(json_encode($response));
  63.     }
  64.     //If we have made it this far without dying, we have successfully added
  65.     //a new user to our database.  We could do a few things here, such as
  66.     //redirect to the login page.  Instead we are going to echo out some
  67.     //json data that will be read by the Android application, which will login
  68.     //the user (or redirect to a different activity, I'm not sure yet..)
  69.     $response["success"] = 1;
  70.     $response["message"] = "Username Successfully Added!";
  71.     echo json_encode($response);
  72.  
  73.     //for a php webservice you could do a simple redirect and die.
  74.     //header("Location: login.php");
  75.     //die("Redirecting to login.php");
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement