首页 > 解决方案 > INSERT 查询适用于 SQL,但不适用于 android 应用程序

问题描述

我正在尝试向我的 SQL 数据库发送一个 INSERT 查询,但它不起作用,也没有出现任何错误,但如果我从 phpMyAdmin 发送查询,则该查询有效。这是我的 PHP 代码:

if ($_POST['func'] == 2) {
$dbca = taskdb();
$dbca->set_charset("utf8");
$mobileUser = $_POST['phone'];
$fullnameUser = $_POST['fullname'];
$usernameUser = $_POST['username'];
$sql = "INSERT INTO users (UserPhone, Username, UserFullname) VALUES (?,?,?)";
$result = $dbca->prepare($sql);
$result->bind_param("sss", $mobileUser,$fullnameUser,$usernameUser);
echo json_encode(array('profileUser' => 'DONE'));
}

这是我在 AsyncT doInBacground 中的 java(client) 代码:

HttpClient httpclient = new DefaultHttpClient();
  HttpPost httppost = new HttpPost("http://192.168.2.26/MyProject/fetchData.php");
  try {
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("func", "2"));
    nameValuePairs.add(new BasicNameValuePair("phone", Login.user_phone));
    nameValuePairs.add(new BasicNameValuePair("fullname", fullname.getText().toString()));
    nameValuePairs.add(new BasicNameValuePair("username", username.getText().toString()));
    Log.e("mainToPost", "mainToPost" + nameValuePairs.toString());
    UrlEncodedFormEntity form;
    form = new UrlEncodedFormEntity(nameValuePairs,"UTF-8");
    httppost.setEntity(form);
    HttpResponse response = httpclient.execute(httppost);
    InputStream inputStream = response.getEntity().getContent();
    Signup.InputStreamToStringExample str = new Signup.InputStreamToStringExample();
    responseSignup = str.getStringFromInputStream(inputStream);
    Log.e("response", "response -----" + responseSignup);
    jsonresponse = new JSONObject(responseSignup);
  } catch (Exception e) {
    e.printStackTrace();
  }
  return null;

我得到“完成”作为客户端的响应,但不会将任何行插入到我的数据库中。任何帮助都感激不尽。

标签: javaphpandroiddatabase

解决方案


添加

$result->execute();

$result->bind_param("sss",  $mobileUser,$fullnameUser,$usernameUser);

在 php 文件中


推荐阅读