【发布时间】:2019-03-21 10:44:30
【问题描述】:
各位开发者,
我正在尝试使用 php 脚本将数据从我的 Android 应用程序插入到我的 MySql 服务器(在 wamp 上运行)。
这些是我的 php 脚本:
<?php
define('hostname','localhost');
define('user','root');
define('password','');
define('databaseName','tutorial');
$connect = mysqli_connect(hostname,user,password,databaseName);
?>
与
<?php
if($_SERVER["REQUEST_METHOD"]=="POST") {
require 'connection.php';
createStudent();
}
function createstudent(){
global $connect;
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$age = $_POST["age"];
$query = "insert into student(firstname,lastname,age) values ('$firstname','$lastname','$age');";
mysqli_query($connect,$query) or die (mysqli_error($connect));
mysqli_close($connect);
}
?>
这是我的安卓代码:
requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> parameters = new HashMap<String,String>();
parameters.put("firstname",et_firstname.getText().toString());
parameters.put("lastname",et_lastname.getText().toString());
parameters.put("age",et_age.getText().toString());
return parameters;
}
};
requestQueue.add(request);
让我说:我知道这是一个很复杂的问题,但我几乎已经结束了我的想法,几周以来一直在寻找解决方案,如果有人可以帮助我,我会很高兴,因为我不是非常喜欢 php 我不能帮助自己太多。
谢谢 你好 亚历克斯
编辑
当我在 Postman 上运行脚本填充数据时,会出现这些错误:
【问题讨论】:
标签: php android android-volley