【发布时间】:2020-04-05 15:50:23
【问题描述】:
我正在开发一个使用 SharedPreferences 登录的应用程序。如果用户想从应用程序中更新他的信息,下一步是添加一个更新数据库的活动。我已经编写了代码,但是当我运行它时,似乎 JSON 有问题。我用断点检查过,值没问题,但是“s=”部分说Error: Call to a member function bind_param() on bool in C:\Users\wamp64\www\App.php on line <i>189</i>所以,我进入我的php并检查了第189行,但我没有发现任何问题。我使用了登录/注册的确切语法,它就像一个魅力。谢谢!
首先,我将添加 .php 中的第 189 行:
$stmt->bind_param("ssss", $nume, $prenume, $email, $telefon);
UpdateInfo.java
class ActualizareUser extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... voids) {
//creating request handler object
RequestHandler requestHandler = new RequestHandler();
//creating request parameters
HashMap<String, String> params = new HashMap<>();
params.put("nume", Nume);
params.put("email", Email);
params.put("prenume", Prenume);
params.put("telefon", Telefon);
params.put("id", id);
//returing the response
return requestHandler.sendPostRequest(URLs.URL_UPDATE_USER, params);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//displaying the progress bar while user registers on the server
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//hiding the progressbar after completion
try {
//converting response to json object
JSONObject objs = new JSONObject(s);
//if no error in response
if (!objs.getBoolean("error")) {
Toast.makeText(getApplicationContext(), objs.getString("message"), Toast.LENGTH_SHORT).show();
//getting the user from the response
JSONObject userJson = objs.getJSONObject("user");
//creating a new user object
User user = new User(
userJson.getString("Nume"),
userJson.getString("Prenume"),
userJson.getString("Adresa_mail"),
userJson.getString("Numar_telefon"),
userJson.getString("Parola"),
userJson.getInt("ID_UTILIZ")
);
//storing the user in shared preferences
SharedPrefManager.getInstance(getApplicationContext()).userLogin(user);
finish();
startActivity(new Intent(getApplicationContext(), Profil.class));
} else {
Toast.makeText(getApplicationContext(), "A aparut o eroare", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
App.php,更新部分:
case 'updateuser':
if(isTheseParametersAvailable(array('nume','email','prenume', 'telefon', 'id'))){
$prenume = $_POST["prenume"];
$nume =$_POST["nume"];
$email =$_POST["email"];
$telefon =$_POST["telefon"];
$id =$_POST["id"];
$stmt = $conn->prepare("SELECT ID_UTILIZ FROM informatii_persoane WHERE ID_UTILIZ = ?");
$stmt->bind_param("s", $id);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows < 1){
$response['error'] = true;
$response['message'] = 'Nu se poate identifica un cont';
$stmt->close();
}else{
$stmt = $conn->prepare("UPDATE INTO informatii_persoane (Nume, Prenume, Adresa_mail, Numar_telefon) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $nume, $prenume, $email, $telefon);
if($stmt->execute()){
$stmt = $conn->prepare("SELECT ID_UTILIZ, Nume, Prenume, Adresa_mail, Numar_telefon, Parola FROM informatii_persoane WHERE Adresa_mail = ?");
$stmt->bind_param("s",$email);
$stmt->execute();
$stmt->bind_result($id, $nume, $prenume, $email, $telefon, $parola);
$stmt->fetch();
$user = array(
'Nume'=>$nume,
'Prenume'=>$prenume,
'Adresa_mail'=>$email,
'Numar_telefon'=>$telefon,
'Parola'=>$parola,
'ID_UTILIZ'=>$id
);
$stmt->close();
$response['error'] = false;
$response['message'] = 'Date actualizate cu success';
$response['user'] = $user;
}
}
}else{
$response['error'] = true;
$response['message'] = 'A aparut o eroare';
}
break;
更新的.php:
case 'updateuser':
if(isTheseParametersAvailable(array('nume','email','prenume', 'telefon', 'id'))){
$prenume = $_POST["prenume"];
$nume =$_POST["nume"];
$email =$_POST["email"];
$telefon =$_POST["telefon"];
$id =$_POST["id"];
$stmt = $conn->prepare("SELECT ID_UTILIZ FROM informatii_persoane WHERE ID_UTILIZ = ?");
$stmt->bind_param("s", $id);
$stmt->execute();
$stmt->store_result();
$rows = $stmt->num_rows;
$stmt->close();
if($rows < 1){
$response['error'] = true;
$response['message'] = 'Nu se poate identifica un cont';
}else{
$stmt = $conn->prepare("UPDATE INTO informatii_persoane (Nume, Prenume, Adresa_mail, Numar_telefon) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $nume, $prenume, $email, $telefon);
if($stmt->execute()){
$stmt1 = $conn->prepare("SELECT ID_UTILIZ, Nume, Prenume, Adresa_mail, Numar_telefon, Parola FROM informatii_persoane WHERE Adresa_mail = ?");
$stmt1->bind_param("s",$email);
$stmt1->execute();
$stmt1->bind_result($id, $nume, $prenume, $email, $telefon, $parola);
$stmt1->fetch();
$stmt1->close();
$user = array(
'Nume'=>$nume,
'Prenume'=>$prenume,
'Adresa_mail'=>$email,
'Numar_telefon'=>$telefon,
'Parola'=>$parola,
'ID_UTILIZ'=>$id
);
$response['error'] = false;
$response['message'] = 'Date actualizate cu success';
$response['user'] = $user;
}
$stmt->close();
}
}else{
$response['error'] = true;
$response['message'] = 'A aparut o eroare';
}
break;
informatii_persoane 结构:
create table if not exists informatii_persoane (
ID_utiliz int auto_increment primary key,
Nume varchar(50),
Prenume varchar(50),
Adresa_mail varchar (50) unique ,
Numar_telefon text ,
Notificare_SMS enum('Da','Nu'),
Notificari_aplicatie enum('Da','Nu'),
Parola varchar(50),
nr_alerte_semnalizate int,
nr_alerte_corecte int,
nr_alerte_partial_corecte int,
nr_lerte_incorecte int, -- vezi aici cum faci %
scor float -- il facem din cele doua de mai sus
);
【问题讨论】:
-
这个错误告诉你你的 $stmt 变量是布尔类型的,但是你试图访问它,就像它是一个对象一样。
-
你在 bind_param 行之前尝试过w3resource.com/php/function-reference/mysqli_error.php 吗?
-
@Costin,分享informatii_persoane Table的结构
-
@AmanjotKaur 我试图在 bind_param 之前添加
$stmt-> error;,但我得到了另一个错误。我认为我没有正确使用它。我用表的结构更新问题 -
@Costin 你试过我最新更新的答案吗..我答案的前两行?