# How to post a data in database

**URL:** <https://forum.ionicframework.com/t/how-to-post-a-data-in-database/137256>\
**Category:** ionic-v1\
**Created:** [August 3, 2018, 9:13am UTC](https://forum.ionicframework.com/t/how-to-post-a-data-in-database/137256 "2018-08-03T09:13:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![partibs](https://avatars.discourse-cdn.com/v4/letter/p/58956e/32.png) [@partibs](https://forum.ionicframework.com/u/partibs)\
**Post date:** [August 3, 2018, 9:13am UTC](https://forum.ionicframework.com/t/how-to-post-a-data-in-database/137256/1 "2018-08-03T09:13:13Z")

</div>

Im using xampp server in windows

---

<div class="post-metadata">

**Author:** ![iamdevesin](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/iamdevesin/32/121040_2.png) [@iamdevesin](https://forum.ionicframework.com/u/iamdevesin)\
**Post date:** [August 14, 2018, 8:10am UTC](https://forum.ionicframework.com/t/how-to-post-a-data-in-database/137256/2 "2018-08-14T08:10:40Z")

</div>

In Ionic v1

create the api directory in www folder of your xampp, then create a file called signup.php

in your controller, put this code

```auto
.controller('registerCtrl', function ($scope, $http, $ionicPopup, $state, $ionicHistory, $ionicLoading) {
    $scope.$on('$ionicView.enter', function () {
      //Clear the Registration Form.
      $scope.user = {
        studentid: '',
        password: '',
        email: '',
        password`Preformatted text`: '',
        firstname: '',
        lastname: ''
      };
    });

    $scope.user = {};
    $scope.signup = function () {
            $ionicLoading.show({
                template: '<ion-spinner icon="android"></ion-spinner>'
            });
            
    var link = 'http://localhost/api-dir/signup.php';
           $scope.studentid = document.getElementById('studentid').value;
           $scope.firstname = document.getElementById('firstname').value;
           $scope.lastname = document.getElementById('lastname').value;
           $scope.email = document.getElementById('email').value;
           $scope.password = document.getElementById('password').value;

        $http.post(link, { studentid: $scope.studentid, firstname: $scope.firstname, lastname: $scope.lastname, email: $scope.email, password: $scope.password})
            .then(function (res) {
               $ionicLoading.hide();
                $scope.response = res.data.result;
                // alert(JSON.stringify($scope.response));
                //alert($scope.response);
                if ($scope.response.created == "1") {
                    $scope.title = "Successful!";
                    $scope.template = "You have successfully created your account";

                    $state.go('login', {}, { location: "replace", reload: true });

                } else if ($scope.response.exists == "1") {
                    $scope.title = "Already Exist";
                    $scope.template = "Student with this information already exist";

                } else {
                    $scope.title = "Failed";
                    $scope.template = "Contact Our Technical Team";
                }
                var alertPopup = $ionicPopup.alert({
                    title: $scope.title,
                    template: $scope.template
                });
            });
        };
    $ionicHistory.nextViewOptions({
      disableAnimate: true,
      disableBack: true
    });
  })

```

and your signup.php can look like this

```auto
<?php
	if (isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400'); // cache for 1 day
    }
    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
        exit(0);
    }

   $postdata = file_get_contents("php://input");

  if (isset($postdata)) {
		$request = json_decode($postdata);
		$studentid=$request->studentid;
		$email=$request->email;
		$password=$request->password;
		$firstname=$request->firstname;
		$lastname=$request->lastname;
		//$password=md5($password);

                $conn = new mysqli("localhost", "dbusername", "dbpassword", "dbname");
		$studentid = stripslashes($studentid);
		$email = stripslashes($email);
		$password = stripslashes($password);
		$firstname = stripslashes($firstname);
		$lastname = stripslashes($lastname);

		$studentid = $conn->real_escape_string($studentid);
		$email = $conn->real_escape_string($email);
		$password = $conn->real_escape_string($password);
		$firstname = $conn->real_escape_string($firstname);
		$lastname = $conn->real_escape_string($lastname);
		
		$datereg = date('d/M/Y');
	
		$check="SELECT count(*) FROM register WHERE student_id = '$studentid'";
		$rs = mysqli_query($conn,$check);
		$data = mysqli_fetch_array($rs, MYSQLI_NUM);

		if($data[0] > 0) {
				$outp='{"result":{"created": "0" , "exists": "1" } }';
		}
		else{
			$sql = "INSERT INTO register (`student_id`,`firstname`,`lastname`,`email`,`password`,`reg_date`) VALUES ('$studentid','$firstname','$lastname','$email','$password','$datereg')";
			if ($conn->query($sql) === TRUE) {

				$outp='{"result":{"created": "1" , "exists": "0" } }';
			}
			else{
				$outp='{"result":{"created": "-1" , "exists": "0" } }';
			}
		}
		echo $outp;
		$conn->close();
}
?>

```

and create your view appropriately

Since you asked the question in Ionic V1 lodge, that’s a **not very clean** way to do it but it works very well

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 9:26pm UTC](https://forum.ionicframework.com/t/how-to-post-a-data-in-database/137256/3 "2021-07-19T21:26:15Z")

</div>

Closed on July 19, 2021, 9:26pm UTC


