Advertisement
Guest User

Untitled

a guest
Apr 16th, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.63 KB | None | 0 0
  1. <?php
  2.  
  3.     /*
  4.         This register.php script was inspired by https://www.w3schools.com/php/php_mysql_insert.asp
  5.     */
  6.  
  7.     //Define and initialize variables to connect to database server
  8.     $servername = "localhost";
  9.     $username = "id1148117_xirokx";
  10.     $password = "project";
  11.     $dbName = "id1148117_login";
  12.    
  13.     //Create new database connection
  14.     $con = mysqli_connect($servername, $username, $password, $dbName);
  15.    
  16.     //check connection - If database connection unsuccessful display error = connection failed
  17.     if ($con->connect_error) {
  18.         die("Connection failed: " . $con->connect_error);
  19.     }
  20.            
  21.    
  22.     //use Php ISSET() function to check that form variables contain values and are not empty
  23.     //when passing data from my login form to this Php script to avoid empty values being stored in database tables
  24.     if(isset($_POST["username"]) && isset($_POST["password"]) && isset($_POST["name"]) && isset($_POST["age"])) {
  25.        
  26.         //define and intialize variables that are to be used with this Php script
  27.         //send ("POST") the data obtained from user form in my application, to my web server
  28.         $name = $_POST["name"];
  29.         $age = $_POST["age"];
  30.         $username = $_POST["username"];
  31.         $password = $_POST["password"];
  32.  
  33.         //Create a "prepare" SQL statement to be used as a template for INSERTing data into database table
  34.         //the database will only accept the values for variables defined above so will protect against SQL Injection
  35.         $statement = mysqli_prepare($con, "INSERT INTO user (name, username, age, password) VALUES (?, ?, ?, ?)");
  36.        
  37.         //attaches ("bind's") my form variables to my SQL query
  38.         mysqli_stmt_bind_param($statement, "ssis", $name, $username, $age, $password);
  39.        
  40.         //executes the SQL query
  41.         //At this point the user form values contained in these variables is INSERTED into my database table
  42.         //under the correct table fields.
  43.         mysqli_stmt_execute($statement);
  44.            
  45.         //declare and initialize a list to store SQL prepared statement result
  46.         $response = array();
  47.        
  48.         //return true and store the word "success" in above list if my SQL statement successfully INSERTED data into my table.
  49.         $response["success"] = true;  
  50.    
  51.         //encode the Php String response as a JSON object which is returned to my mobile application
  52.         //a JSON object is returned so my mobile application can easily read and work with it as a "String"
  53.         echo json_encode($response);
  54.        
  55.     }
  56.     //if form variables are empty then respond with an error
  57.     else {
  58.         echo json_encode ("please ensure all form values are complete to proceed");    
  59.     }
  60.    
  61.     //Close database connection to service further db requests and
  62.     //avoid application from slowing down
  63.     $conn->close();
  64. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement