INSERT查询可在SQL中使用,但不适用于Android应用

时间:2018-09-15 11:53:49

标签: java php android database

我正在尝试向我的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'));
}

这是我的java(client)代码,位于AsyncT doInBacground内:

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;

我在客户端得到“ DONE”作为响应,但是没有行会插入到我的数据库中。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

添加

$result->execute();

之后

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

在php文件中