我已经开始研究一个项目,但是我已经在这里,我需要为我已经存储在我的数据库中的每个用户添加点数。我创建了一个数据库,我为用户存储用户名和密码,并将其连接到android。一切都在工作,但现在在我的情况下,我想为用户存储积分,例如在我的应用程序中解决了一些问题,但我不知道如何为此编写方法,需要在php中做一点帮助这一点。
这些是我的php文件: Register.php
<?php
$con=mysqli_connect("localhost", "root", "", "geomondo");
$username = $_POST["username"];
$password = $_POST["password"];
//$username = "admin";
//$password = "admin";
$statement = mysqli_prepare($con, "INSERT INTO User (username, password) VALUES (?, ?)");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);
mysqli_close($con);
?>
还有一个: FetchUserData.php
<?php
$con=mysqli_connect("localhost", "root", "", "geomondo");
$username = $_POST["username"];
$password = $_POST["password"];
//$username = "test";
//$password = "test";
$statement = mysqli_prepare($con, "SELECT * FROM User WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $username, $password);
$user = array();
while(mysqli_stmt_fetch($statement)){
$user["username"] = $username;
$user["password"] = $password;
}
echo json_encode($user);
mysqli_close($con);
?>
那么我现在如何在这里包括每个用户的积分?
这是我用于将android连接到php和mysql的java文件:
public class ServerRequests {
ProgressDialog progressDialog;
public static final int CONNECTION_TIMEOUT = 1000 * 15;
public static final String SERVER_ADRESS = "http://192.168.0.17:8081/project/";
public ServerRequests(Context context){
progressDialog = new ProgressDialog(context);
progressDialog.setCancelable(false);
progressDialog.setTitle(R.string.progress_dialog);
progressDialog.setMessage("Please wait...");
}
public void storeUserDataInBackground(User user, GetUserCallback userCallback){
progressDialog.show();
new StoreUserDataAsyncTask(user, userCallback).execute();
}
public void fetchUserDatainBackground(User user, GetUserCallback callBack){
progressDialog.show();
new fetchUserDataAsyncTask(user, callBack).execute();
}
public class StoreUserDataAsyncTask extends AsyncTask<Void, Void, Void>{
User user;
GetUserCallback userCallback;
public StoreUserDataAsyncTask(User user, GetUserCallback userCallback){
this.user = user;
this.userCallback = userCallback;
}
@Override
protected Void doInBackground(Void... params) {
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("username", user.username));
dataToSend.add(new BasicNameValuePair("password", user.password));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADRESS + "Register.php");
try {
post.setEntity(new UrlEncodedFormEntity(dataToSend));
client.execute(post);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
progressDialog.dismiss();
userCallback.done(null);
super.onPostExecute(aVoid);
}
}
public class fetchUserDataAsyncTask extends AsyncTask<Void, Void, User> {
User user;
GetUserCallback userCallback;
public fetchUserDataAsyncTask(User user, GetUserCallback userCallback) {
this.user = user;
this.userCallback = userCallback;
}
@Override
protected User doInBackground(Void... params) {
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("username", user.username));
dataToSend.add(new BasicNameValuePair("password", user.password));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADRESS + "FetchUserData.php");
User returnedUser = null;
try {
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jObject = new JSONObject(result);
if (jObject.length() == 0)
{
returnedUser = null;
}
else
{
returnedUser = new User(user.username, user.password);
}
}catch (Exception e){
e.printStackTrace();
}
return returnedUser;
}
@Override
protected void onPostExecute(User returnedUser) {
progressDialog.dismiss();
userCallback.done(returnedUser);
super.onPostExecute(returnedUser);
}
}
}
这是我第一次将android连接到在线数据库,我还在学习android开发,所以我会在这里给予任何帮助。 :)
答案 0 :(得分:0)
您的问题表明您在基本条款方面存在一些问题。 Study it。 当您创建一个帐户时(顺便说一下,数据库最好不要存储password but its hash),您可以记录完全相同:发送用户名和密码。如果它存在于数据库中创建session in php。
//here we are looking for a user in the database. I do not know how to work with mysqli
session_start();
$_SESSION['name'] = $login;
(better use id to store the user's session)
现在,您将能够根据用户的请求(通过会话)确定
if ($_SESSION['name'] != '')
并将数据添加到表格中。
id | login | score
这是最简单的解决方案。祝你好运。