0

I want to insert data from 2 forms to my database using the PHP API. But I got an error. Please help me to fix this.

My form : testing_form.html

<ion-view view-title="Form Insert">
<ion-content>
    <div class="list">
        <div ng-app="myApp" ng-controller="cobaInsert" >
          <label class="item item-input">
            <input type="text" placeholder="Username" ng-model="username">
          </label>
          <label class="item item-input">
            <input type="password" placeholder="Password" ng-model="password">
          </label>
          <button class="button button-block button-positive" ng-click="insertData()">
              Submit
            </button>
        </div>
    </div>
</ion-content>

my controller : controller.js

angular.module('myApp',[]);
.controller('cobaInsert',function($scope, $stateParams, $http){    
        $scope.insertData=function(){      
            $http.post('http://pvcmate.com/api/coba_form.php', {
                'username':$scope.username,
                'password':$scope.password
            }).then(function(response){
                    console.log("Data Inserted Successfully");
                },function(error){
                    alert("Sorry! Data Couldn't be inserted!");
                    console.error(error);
                });
            }
        });

This is my php file : coba_form.php

<?php 
$data = json_decode(file_get_contents("php://input"));
// $bname = mysql_real_escape_string($data->bname);
// $bauthor = mysql_real_escape_string($data->bphone);

$username = $data->username;
$password = $data->password;


mysql_connect("localhost", "root", "password"); 
mysql_select_db("testing");

mysql_query("INSERT INTO 'coba_form'('','username','password')
    VALUES ('".$username."','".$password."') ") or die(mysql_error());
//echo $bname." ".$bphone;
?>
Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51
  • What error did you get? – Giovani Vercauteren Dec 19 '16 at 15:22
  • If you're writing new code, **_please_ don't use the `mysql_*` functions**. They are old and broken, were deprecated in PHP 5.5 (which is so old it no longer even receives security updates), and completely removed in PHP 7. Use [`PDO`](https://secure.php.net/manual/en/book.pdo.php) or [`mysqli_*`](https://secure.php.net/manual/en/book.mysqli.php) with _prepared statements_ and _parameter binding_ instead. See http://stackoverflow.com/q/12859942/354577 for details. – ChrisGPT was on strike Dec 22 '16 at 12:55

2 Answers2

0

You didn't add model to ng-click="insertData()".

Add username and password to ng-click like this ng-click="insertData(username,password)"

or make models like this ng-model="something.username", ng-model="something.password" and add something to ng-click like this ng-click="insertData(something)".

P.S. Use mysqli instead of mysql. - More about this.

Community
  • 1
  • 1
Tomislav Stankovic
  • 3,080
  • 17
  • 35
  • 42
  • Hello.. Im done change it with your recomend code. I insert data to 2 form and click submit but now the console have output like this : XMLHttpRequest cannot load http://pvcmate.com/api/coba_form.php. Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response. What is that? Thanks.. – Yosia Prabowo Dec 29 '16 at 14:07
  • @YosiaPrabowo Add CORS plugin https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en to Chrome, enable it, and try again. – Tomislav Stankovic Dec 29 '16 at 15:52
  • @tomyslav Amazing, its work well insert to my database. But after I click insert button, system store 2 data to database and username+password is null. Just id can inserted to my database. – Yosia Prabowo Dec 29 '16 at 17:14
  • @YosiaPrabowo Did you change `mysql` to `mysqli`? And, do you get correct values in `insertData` function when console.log `username` and `password`? – Tomislav Stankovic Dec 29 '16 at 17:18
  • Yes, I use mysqli. Its wrong? In log console I just get "Data Inserted Successfully" output.. – Yosia Prabowo Dec 29 '16 at 17:27
  • sorry, in my question I read with mysql. Exactly I use mysqli in my program. I will write again in new answer. Thanks – Yosia Prabowo Dec 29 '16 at 17:45
  • @YosiaPrabowo Firstly, we need to evaluate if client side working properly. Try to console.log `$scope.username` and `$scope.password` inside `insertData()` function in controller.js to see if you get correct values that you entered in the form. P.S. You can edit your question and add your PHP there instead as answer. – Tomislav Stankovic Dec 29 '16 at 18:27
  • still error. Is it possible if I have an error during the installation nodejs, and others? Because I had a problem on my url. Url my current http://192.168.1.8:8100/ and instead of http: // localhost: 8100 / – Yosia Prabowo Dec 31 '16 at 13:10
0

This is my PHP

<?php 
header("Access-Control-Allow-Origin: *");
//header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
$kode_decode = file_get_contents("php://input")
$data = json_decode($kode_decode);
$username = mysql_real_escape_string($data->username);
$password = mysql_real_escape_string($data->password);

//$foo= $_GET['first'];
//$foo1= $_GET['second'];

$host = "mysql.idhostinger.com";
$username_database = "";
$pass_database = "";
$database = "private";

$connect_db = mysqli_connect($host, $username_database, $pass_database, $database); 

if ($connect_db) {
    echo "Database ditemukan";
}else {
  echo "Database Tidak Ada";
  mysqli_close($connect_db);
}

$tesa = 'yosia';
$tesb = 'pazz';

$query = "INSERT INTO coba_form VALUES('','".$tesa."','".$tesb."')";
$sql = mysqli_query($connect_db, $query);

if($sql){
    echo "data berhasil di insert ke dalam database";
}else{
    echo "data gagal diinsert ke dalam database";
}
//mysql_select_db("300927381_smst");
/*$tesa = 'yosia';
$tesb = 'pazz';
$qry = 'INSERT INTO coba_form values ("''","'.$tesa.'","'.$tesb.'")';



$qry_res = mysql_query($qry);

if ($qry_res) {
    $arr = array('msg' => "User Created Successfully!!!", 'error' => '');
    $jsn = json_encode($arr);
    print_r($jsn);
} else {
    $arr = array('msg' => "", 'error' => 'Error In inserting');
    $jsn = json_encode($arr);
    print_r($jsn);
}*/

?>

What is wrong? Thanks