java - 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;
我得到“完成”作为客户端的响应,但不会将任何行插入到我的数据库中。任何帮助都感激不尽。
解决方案
添加
$result->execute();
后
$result->bind_param("sss", $mobileUser,$fullnameUser,$usernameUser);
在 php 文件中
推荐阅读
- android - MutableLiveData 可以有一个子类作为它的类型吗?
- mysql - 需要从项目表中获取所有项目,包括所有提供的标签 ID WITH SEQUELIZE
- python - 循环在装饰器中如何工作(记忆)
- c++ - 在访问 struct 的不同属性的 C++ 函数中重用代码
- cucumber - 在 HTML 编辑器中插入文本
- python - 如何使用 selenium 抓取 Whatsapp 网页的元素
- reactjs - 带有双破折号的 CSS 选择器
- wpf - 如何提供静态资源作为类的值
? - c++ - 无法更新类内字段的值
- laravel - Laravel:集合中的 OrderBy 嵌套对象