douying0108 2019-03-16 23:23
浏览 76

我想根据登录的用户名使用php更新数据库中的字段,但它无法正常工作

I am working on an Android project and I am using JSON and PHP. I am having a trouble in updating a field in the database depending on the current logged in user username.

Here is the PHP code:

$stmt=$this->con->prepare("UPDATE buses SET gpsLink = ? WHERE username=? ");


$stmt->bind_param("ss",$gpsLink,$username);
if ($stmt->execute())
{


$stmt->bind_param("ss",$gpsLink,$username);
return 1;}
else 
{return 2;}

Then I made a class called user to take the information of the current logged in user like this:

public class User {
String username;
String fullName;
Date sessionExpiryDate;


int id;
public void setUsername(String username) {
    this.username = username;
}
public void setId(int id) {
    this.id = id;
}

public void setFullName(String fullName) {
    this.fullName = fullName;
}


public void setSessionExpiryDate(Date sessionExpiryDate) {
    this.sessionExpiryDate = sessionExpiryDate;
}

public String getUsername() {
    return username;
}
public int getId() {
    return id;
}

public String getFullName() {
    return fullName;
}



public Date getSessionExpiryDate() {
    return sessionExpiryDate;
}
}

and here is the code in Android studio

private String username;
 session = new SessionHandler(getApplicationContext());
    User user = session.getUserDetails();
username=user.getFullName();
public void enterLocation()


{



    final String Location=location.getText().toString().trim();
    final String theUsername=username;
    Toast.makeText(getApplicationContext(),theUsername,Toast.LENGTH_SHORT).show();


//        progressDialog.setMessage("Sending your location ... ");


    StringRequest stringRequest=new StringRequest(Request.Method.POST,
            enter_location_url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {



            try {
                JSONObject jsonObject=new JSONObject(response);
                Toast.makeText(getApplicationContext(),jsonObject.getString("message"),Toast.LENGTH_LONG).show();
            } catch (JSONException e) {
                e.printStackTrace();
            }


        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    Toast.makeText(getApplicationContext(),error.getMessage(),Toast.LENGTH_LONG).show();

                }
            }
    ){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map <String,String> params=new HashMap<>();
            params.put("gpsLink",Location);
            params.put("username",theUsername);


            return params;

        }
    };
    RequestQueue requestQueue= Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

here is the class SessionHandler:

public class SessionHandler {
  private static final String PREF_NAME = "UserSession";
private static final String KEY_USERNAME = "username";
private static final String KEY_EXPIRES = "expires";
private static final String KEY_FULL_NAME = "full_name";
private static final String KEY_EMAIL = "email";
private static final String KEY_EMPTY = "";
private Context mContext;
private SharedPreferences.Editor mEditor;
private SharedPreferences mPreferences;

public SessionHandler(Context mContext) {
    this.mContext = mContext;
    mPreferences = mContext.getSharedPreferences(PREF_NAME,     Context.MODE_PRIVATE);
    this.mEditor = mPreferences.edit();
}

public User getUserDetails() {
    //Check if user is logged in first
    if (!isLoggedIn()) {
        return null;
    }
    User user = new User();
    user.setUsername(mPreferences.getString(KEY_USERNAME, KEY_EMPTY));
    user.setFullName(mPreferences.getString(KEY_FULL_NAME, KEY_EMPTY));

    user.setSessionExpiryDate(new Date(mPreferences.getLong(KEY_EXPIRES, 0)));

    return user;
}

Note that in the PHP file if I write WHERE username='Sarah ' it works but I want it to take it from the app itself.

  • 写回答

1条回答 默认 最新

  • dsztc99732 2019-03-16 23:33
    关注

    If you say that this is working

    WHERE username='Sarah '
    

    Have you tried putting the ? inside '?" like this

    $stmt=$this->con->prepare("UPDATE buses SET gpsLink = ? WHERE username='?'");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么