if($ _ SERVER [' REQUEST_METHOD'] ==' POST')无效。数据不是从数据库中恢复的

时间:2017-09-03 19:28:06

标签: php

我在Android中创建了一个登录页面。第一个用户将注册,然后他将重定向到登录页面,他必须在其中输入这些详细信息作为名称和ID。再加上这个名字& id将显示在另一个Android活动上。问题是用户注册正确。但是在重温这个名字的同时id它给了我错误" {"状态":" false","消息":"发生错误,请再试一次! "} "如果我做错了,请告诉我。我使用这个与Android文件。这些名字和地点id将显示。

代码:

abc.php
<?php

   if($_SERVER['REQUEST_METHOD']=='POST'){
  // echo $_SERVER["DOCUMENT_ROOT"];  // /home1/demonuts/public_html
//including the database connection file
       include_once("dbConfig.php");

       $mi_id = isset($_POST['mi_id']) ? $_POST['mi_id'] : null;
      $name = isset($_POST['name']) ? $_POST['name'] : null;

     if( $mi_id == '' || $name == '' ){
            echo json_encode(array( "status" => "false","message" => "Parameter missing!") );
     }else{
        $query= "SELECT * FROM registerdemo WHERE mi_id='$mi_id' AND name='$name'";
            $result= mysqli_query($con, $query);

            if(mysqli_num_rows($result) > 0){  
             $query= "SELECT * FROM registerdemo WHERE mi_id='$mi_id' AND name='$name'";
                         $result= mysqli_query($con, $query);
                     $emparray = array();
                         if(mysqli_num_rows($result) > 0){  
                         while ($row = mysqli_fetch_assoc($result)!=NULL) {
                                     $emparray[] = $row;
                                   }
                         }
               echo json_encode(array( "status" => "true","message" => "Login successfully!", "data" => $emparray) );
            }else{ 
                echo json_encode(array( "status" => "false","message" => "Invalid username or password!") );
            }
             mysqli_close($con);
     }
    } 
    else
    {
            echo json_encode(array( "status" => "false","message" => "Error occured, please try again!") );
    }
?>

Java代码      Welcome.java     package com.exampledemo.sample.registerloginsession;

import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class WelcomeActivity extends AppCompatActivity {

    private TextView tvname,tvmi_id;
    private Button btnlogout;
    private PreferenceHelper preferenceHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        preferenceHelper = new PreferenceHelper(this);

        tvmi_id = (TextView) findViewById(R.id.tvmi_id);
        tvname = (TextView) findViewById(R.id.tvname);
        btnlogout = (Button) findViewById(R.id.btn);

        tvname.setText(preferenceHelper.getName());
        tvmi_id.setText(getResources().getString(R.string.welcome_message, preferenceHelper.getMi_id()));


        btnlogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                preferenceHelper.putIsLogin(false);
                Intent intent = new Intent(WelcomeActivity.this,See_Feedback.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                WelcomeActivity.this.finish();
            }
        });

    }
}

Login.java
package com.exampledemo.sample.registerloginsession;

import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class WelcomeActivity extends AppCompatActivity {

    private TextView tvname,tvmi_id;
    private Button btnlogout;
    private PreferenceHelper preferenceHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        preferenceHelper = new PreferenceHelper(this);

        tvmi_id = (TextView) findViewById(R.id.tvmi_id);
        tvname = (TextView) findViewById(R.id.tvname);
        btnlogout = (Button) findViewById(R.id.btn);

        tvname.setText(preferenceHelper.getName());
        tvmi_id.setText(getResources().getString(R.string.welcome_message, preferenceHelper.getMi_id()));


        btnlogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                preferenceHelper.putIsLogin(false);
                Intent intent = new Intent(WelcomeActivity.this,See_Feedback.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                WelcomeActivity.this.finish();
            }
        });

    }
}

1 个答案:

答案 0 :(得分:1)

你的代码写得不好;我试着解决它。

请注意,我强烈建议使用预准备语句来保护您免受MySQL注入

请尝试以下代码:

<?php
    //Don't run code until POST request is received
   if($_SERVER['REQUEST_METHOD']=='POST'){
    include_once("dbConfig.php");

      //Extract content of POST request into variables $mi_id, $name
      $mi_id = isset($_POST['mi_id']) ? $_POST['mi_id'] : null;
      $name = isset($_POST['name']) ? $_POST['name'] : null;

     //Don't run next code if any of these variables 
     //is empty null or false; return message
     if( empty( $mi_id ) || empty( $name ) ){
            echo json_encode(array( "status" => "false","message" => "Parameter missing!") );
     }

    //If all you made it here, let's run the query; 
    $query= "SELECT * FROM registerdemo WHERE mi_id='$mi_id' AND name='$name'";

    //Only if there's result and greater than 0, then return something
    if( $result= mysqli_query($con, $query)) {

            if(mysqli_num_rows($result) > 0){  
                $emparray = array();
                    while ($row = mysqli_fetch_assoc($result)!=NULL) {
                        $emparray[] = $row;
                    }

                echo json_encode(array( "status" => "true","message" => "Login successfully!", "data" => $emparray) );
            }
            else{ 
                echo json_encode(array( "status" => "false","message" => "Invalid username or password!") );
            }
    }
    else {
        echo json_encode(array( "status" => "false","message" => "Error occured, please try again!") );
    }
    // Close mysql connection;
     mysqli_close($con);
   }
?>

希望这有帮助。