输入值未从Ionic传递到我的SQL数据库

时间:2018-09-25 10:36:34

标签: php ionic3

我正在从事这个项目,我在其中将值从Ionic(TypeScript)文件传递到MySQL数据库。我的PHP在邮递员中给了我这个错误:

<br />
<b>Warning</b>:  mysqli_num_rows() expects exactly 1 parameter, 2 given in
<b>C:\xampp2\htdocs\authapp\ddregistration.php</b> on line
<b>19</b>
<br />
insert into info_user values ('','','','');
<br />
<b>Notice</b>:  Trying to get property of non-object in
<b>C:\xampp2\htdocs\authapp\ddregistration.php</b> on line
<b>33</b>
<br />
<br />
<b>Notice</b>:  Trying to get property of non-object in
<b>C:\xampp2\htdocs\authapp\ddregistration.php</b> on line
<b>33</b>
<br />
[[null,null]]

我不知道为什么值没有被传递。即使我没有来自php的警告和注意,情况也是如此。我正在显示错误日志,以便可以正确纠正错误。

这是我的register.ts文件

import { Component, ViewChild } from '@angular/core';

import { NavController, AlertController } from 'ionic-angular';

import { HomePage } from '../home/home';

import {Http, Headers, RequestOptions}  from "@angular/http";

import { LoadingController } from 'ionic-angular';

import { Device } from '@ionic-native/device';

import 'rxjs/add/operator/map';

@Component({

selector: 'page-register',

templateUrl: 'register.html'

})

export class RegisterPage {



@ViewChild("name") name;

@ViewChild("phone") phone;

@ViewChild("imei") imei;

@ViewChild("status") status;


constructor(public navCtrl: NavController, public alertCtrl: AlertController,
      private http: Http, private device: Device, public loading: LoadingController) {

}

Register(){

//// check to confirm the username, email, telephone and password fields are filled

if(this.name.value=="" ){

let alert = this.alertCtrl.create({

title:"ATTENTION",

subTitle:"Name field is empty",

buttons: ['OK']

});

alert.present();

} else


if(this.phone.value=="" ){

let alert = this.alertCtrl.create({

title:"ATTENTION",

subTitle:"Mobile number field is empty",

buttons: ['OK']

});

alert.present();

}

else

{

var headers = new Headers();

headers.append("Accept", 'application/json');

headers.append('Content-Type', 'application/json' );

let options = new RequestOptions({ headers: headers });

var data = {

name: this.name.value,

phone: this.phone.value,

imei: this.device.serial,

status:1,


};


let loader = this.loading.create({

content: 'Processing please wait…',

});

loader.present().then(() => {
    var myData = JSON.stringify({name: this.name.value,phone: this.phone.value,
        imei: this.device.serial,status:1});
        console.log("Before",myData);    
this.http.post('http://localhost:82/authapp/ddregistration.php',myData, options)

.map(res => res.json())

.subscribe(res => {

loader.dismiss()

if(res=="Registration successfull"){

let alert = this.alertCtrl.create({

title:"CONGRATS",

subTitle:(res),

buttons: ['OK']

});

alert.present();

this.navCtrl.push(HomePage);

}else

{

let alert = this.alertCtrl.create({

title:"ERROR",

subTitle:(res),

buttons: ['OK']

});

alert.present();

}

});

});

}

}

}

这是我的php脚本

   <?php
require "dbconnect.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);

    }

$name = mysqli_real_escape_string($con,$_POST['name']);
$phone = mysqli_real_escape_string($con,$_POST['phone']);
$imei = mysqli_real_escape_string($con,$_POST['imei']);
$status = mysqli_real_escape_string($con,$_POST['status']);

$sql = "select * from info_user where dboy_device_info like '".$imei."';";

$result = mysqli_query($con,$sql);
$response = array();

if(mysqli_num_rows($result)>0)
{
    $code = "reg_failed";
    $message = "User Exists";
    $response = ['code' => $code, 'message' => $message];

    echo json_encode($message);
}
else
{
    $sql = "insert into info_user (dboy_name,dboy_phone,dboy_device_info,dboy_status)values ('".$name."','".$phone."','".$imei."','".$status."');";

    $result = mysqli_query($con,$sql);
    $code = "reg_success";
    $message = "Registration Success";
    $response = ['code' => $code, 'message' => $message];

    echo json_encode($message);
}

?>

我是一个初学者,正在学习。

1 个答案:

答案 0 :(得分:0)

我认为您不需要这个

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

像这样直接访问数据

$name = mysqli_real_escape_string($con,$_POST['name']);