当我将值插入数据库时,代码工作正常。我使用
时代码有效select * from tblLogin
但是当我从android代码中获取$ _POST值并尝试运行
时SELECT * FROM tblLogin WHERE username='$username' AND password='$password'
声明它不能正常工作。
PHP
<?php
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$username = $_POST['username'];
$password = $_POST['password'];
$db = mysql_connect("localhost", "root", "");
mysql_select_db("ntu", $db);
$sql = mysql_query("SELECT * FROM tblLogin WHERE username='$username' AND password='$password'");
$a = array();
$index = 0;
while($row = mysql_fetch_assoc($sql))
{
$a[$index] = $row;
$index++;
}
echo json_encode(array("students"=>$a));
mysql_close($db);
}
?>
安卓代码
package com.example.digi.studentapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
EditText username,password;
Button login,register;
RequestQueue requestQueue;
//String insert = "http://192.168.1.104/android/insert.php";
String checklogin = "http://192.168.1.104/android/login.php";
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText) findViewById(R.id.editText);
password = (EditText) findViewById(R.id.editText2);
login = (Button) findViewById(R.id.button);
register = (Button) findViewById(R.id.button2);
result = (TextView) findViewById(R.id.textView);
requestQueue = Volley.newRequestQueue(getApplicationContext());
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
checklogin, new Response.Listener<JSONObject>() {
public void onResponse(JSONObject response) {
try {
JSONArray students = response.getJSONArray("students");
for (int i = 0; i < students.length(); i++) {
JSONObject tblLogin = students.getJSONObject(i);
String id = tblLogin.getString("id");
String username = tblLogin.getString("username");
String password = tblLogin.getString("password");
result.append(id+" "+username+" "+password+"\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonObjectRequest);
}
});
}
}
答案 0 :(得分:0)
我不是php的专家,但username='$username'
似乎错了!
试试username='.$username.'
对您要在请求中使用的变量执行相同的操作。
答案 1 :(得分:0)
使用以下命令调试您的SQL查询:
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
$query = "SELECT * FROM tblLogin WHERE username='".$username."' AND password='".$password."'";
$sql = mysql_query($query);
if (!$sql)
{
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
请!停止使用mysql_query,它已弃用,您的方法会使您的应用程序易受攻击
<强> EDITED 强> 您可以使用Postman
之类的内容在应用外部发出POST请求来测试结果