Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用android studio和php更新mysql数据库中的点_Java_Php_Android_Mysql - Fatal编程技术网

Java 使用android studio和php更新mysql数据库中的点

Java 使用android studio和php更新mysql数据库中的点,java,php,android,mysql,Java,Php,Android,Mysql,我目前有一个有效的登录/注册系统。当用户登录并单击一个按钮时,用户名会被带到Points.php文件中。然后,php将获取用户名并向该特定用户点列添加+5点。如果你能帮忙,我会非常感激,谢谢! 这是我目前正在尝试使用的: activity_user_area.xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/a

我目前有一个有效的登录/注册系统。当用户登录并单击一个按钮时,用户名会被带到Points.php文件中。然后,php将获取用户名并向该特定用户点列添加+5点。如果你能帮忙,我会非常感激,谢谢! 这是我目前正在尝试使用的:

activity_user_area.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/activity_horizontal_margin">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/tvWelcomeMsg"
        android:text="Username"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etUsername"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView"
        android:editable="false" />

    <TextView
        android:id="@+id/tvWelcomeMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:text="Earn Points"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignRight="@+id/tvWelcomeMsg"
        android:layout_alignEnd="@+id/tvWelcomeMsg"
        android:id="@+id/bEarn"/>

</RelativeLayout>

UserAreaActivity.java:

import android.app.AlertDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class UserAreaActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_area);

        Intent intent = getIntent();
        final String username = intent.getStringExtra("username");

        final Button bEarn = (Button) findViewById(R.id.bEarn);

        TextView tvWelcomeMsg = (TextView) findViewById(R.id.tvWelcomeMsg);
        final EditText etUsername = (EditText) findViewById(R.id.etUsername);

        // Display user details
        String message = "Welcome, " + username + "!";
        tvWelcomeMsg.setText(message);
        etUsername.setText(username);

        bEarn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String username = etUsername.toString();

                Response.Listener<String> responseListener = new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success = jsonResponse.getBoolean("success");
                            if (success) {
                                AlertDialog.Builder builder = new AlertDialog.Builder(UserAreaActivity.this);
                                builder.setMessage("Points Added")
                                        .create()
                                        .show();
                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(UserAreaActivity.this);
                                builder.setMessage("Points Not Added")
                                        .setNegativeButton("Retry", null)
                                        .create()
                                        .show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };

                EarnRequest earnRequest = new EarnRequest(username, responseListener);
                RequestQueue queue = Volley.newRequestQueue(UserAreaActivity.this);
                queue.add(earnRequest);
            }
        });
    }
}
导入android.app.AlertDialog;
导入android.content.Intent;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.toolbox.volley;
导入org.json.JSONException;
导入org.json.JSONObject;
公共类UserAreaActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u user\u区域);
Intent=getIntent();
最终字符串用户名=intent.getStringExtra(“用户名”);
最终按钮bEarn=(按钮)findViewById(R.id.bEarn);
TextView tvWelcomeMsg=(TextView)findViewById(R.id.tvWelcomeMsg);
最终EditText etUsername=(EditText)findViewById(R.id.etUsername);
//显示用户详细信息
字符串消息=“欢迎,”+username+“!”;
tvwelcomesg.setText(消息);
etUsername.setText(用户名);
bEarn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
最终字符串username=etUsername.toString();
Response.Listener responseListener=新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
试一试{
JSONObject jsonResponse=新的JSONObject(响应);
boolean success=jsonResponse.getBoolean(“success”);
如果(成功){
AlertDialog.Builder=新建AlertDialog.Builder(UserAreaActivity.this);
builder.setMessage(“添加的点”)
.create()
.show();
}否则{
AlertDialog.Builder=新建AlertDialog.Builder(UserAreaActivity.this);
builder.setMessage(“未添加点”)
.setNegativeButton(“重试”,null)
.create()
.show();
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
};
EarnRequest EarnRequest=新的EarnRequest(用户名、响应者);
RequestQueue=Volley.newRequestQueue(UserAreaActivity.this);
queue.add(请求);
}
});
}
}
EarnRequest.java:

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

public class EarnRequest extends StringRequest {
    private static final String EARN_REQUEST_URL = "https://mysite567356ht7ieyjr6u7je.000webhostapp.com/Points.php";
    private Map<String, String> params;

    public EarnRequest(String username, Response.Listener<String> listener) {
        super(Method.POST, EARN_REQUEST_URL, listener, null);
        params = new HashMap<>();
        params.put("username", username);
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }
}
import com.android.volley.Response;
导入com.android.volley.toolbox.StringRequest;
导入java.util.HashMap;
导入java.util.Map;
公共类EarnRequest扩展了StringRequest{
私有静态最终字符串EARN\u REQUEST\u URL=”https://mysite567356ht7ieyjr6u7je.000webhostapp.com/Points.php";
私有映射参数;
公共请求(字符串用户名,Response.Listener){
super(Method.POST,EARN\u REQUEST\u URL,listener,null);
params=新的HashMap();
参数put(“用户名”,用户名);
}
@凌驾
公共映射getParams(){
返回参数;
}
}
Points.php:

<?php
$con = mysqli_connect("localhost", "id177667_root", "***", "id177667_loginb");
$username = $_POST["username"];
$statement = mysqli_prepare($con, "UPDATE user SET points = points + 5 WHERE username_column =".$username);
mysqli_stmt_bind_param($statement, "s", $username);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;  

echo json_encode($response);
?>

首先在php文件中使用

Select points from user where username_column =$username
然后使用update查询语句

$result=$result + 5;

 UPDATE user SET points = $result WHERE username_column =$username

应该是
。。其中username\u column=?
在prepare语句中(占位符
而不是
$username
)。你没有做任何错误处理,你只是假设一切都很顺利。那么,你说我的php应该是这样的<代码>mysqli_prepare($con,“更新用户设置点=点+5,其中用户名_列=?”)像这样,看看引号。