Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- This register.php script was inspired by https://www.w3schools.com/php/php_mysql_insert.asp
- */
- //Define and initialize variables to connect to database server
- $servername = "localhost";
- $username = "id1148117_xirokx";
- $password = "project";
- $dbName = "id1148117_login";
- //Create new database connection
- $con = mysqli_connect($servername, $username, $password, $dbName);
- //check connection - If database connection unsuccessful display error = connection failed
- if ($con->connect_error) {
- die("Connection failed: " . $con->connect_error);
- }
- //use Php ISSET() function to check that form variables contain values and are not empty
- //when passing data from my login form to this Php script to avoid empty values being stored in database tables
- if(isset($_POST["username"]) && isset($_POST["password"]) && isset($_POST["name"]) && isset($_POST["age"])) {
- //define and intialize variables that are to be used with this Php script
- //send ("POST") the data obtained from user form in my application, to my web server
- $name = $_POST["name"];
- $age = $_POST["age"];
- $username = $_POST["username"];
- $password = $_POST["password"];
- //Create a "prepare" SQL statement to be used as a template for INSERTing data into database table
- //the database will only accept the values for variables defined above so will protect against SQL Injection
- $statement = mysqli_prepare($con, "INSERT INTO user (name, username, age, password) VALUES (?, ?, ?, ?)");
- //attaches ("bind's") my form variables to my SQL query
- mysqli_stmt_bind_param($statement, "ssis", $name, $username, $age, $password);
- //executes the SQL query
- //At this point the user form values contained in these variables is INSERTED into my database table
- //under the correct table fields.
- mysqli_stmt_execute($statement);
- //declare and initialize a list to store SQL prepared statement result
- $response = array();
- //return true and store the word "success" in above list if my SQL statement successfully INSERTED data into my table.
- $response["success"] = true;
- //encode the Php String response as a JSON object which is returned to my mobile application
- //a JSON object is returned so my mobile application can easily read and work with it as a "String"
- echo json_encode($response);
- }
- //if form variables are empty then respond with an error
- else {
- echo json_encode ("please ensure all form values are complete to proceed");
- }
- //Close database connection to service further db requests and
- //avoid application from slowing down
- $conn->close();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement