Java Android-PHPMYSQL。没有更新的值
我是android和php语言的新手。我想在安卓系统中点击一个按钮,为学生们增加分数。我的数据库表是“user”,我希望更新它的列是“point” 我尝试将此代码用于point.php,它将在android的onclick按钮中激活Java Android-PHPMYSQL。没有更新的值,java,php,android,mysql,Java,Php,Android,Mysql,我是android和php语言的新手。我想在安卓系统中点击一个按钮,为学生们增加分数。我的数据库表是“user”,我希望更新它的列是“point” 我尝试将此代码用于point.php,它将在android的onclick按钮中激活 <?php if($_SERVER['REQUEST_METHOD']=='POST'){ //Getting values $point = $_POST['point_add']; $myVar = $point; var_dump(
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
//Getting values
$point = $_POST['point_add'];
$myVar = $point;
var_dump($myVar);
$myVar= $myVar += 1;
var_dump($myVar);
//importing database connection script
require "init.php";
//Creating sql query
$sql = "UPDATE user SET point = '$point' WHERE id = $id;";
//Updating database table
if(mysqli_query($con,$sql)){
echo 'Data Updated Successfully';
}else{
echo 'Could Not Update Data Try Again';
}
//closing connection
mysqli_close($con);
}
?>
这是我的init.php
<?php
error_reporting(0);
$db_name = "mymerit";
$mysql_user = "root";
$mysql_pass = "root";
$server_name = "localhost";
$con = mysqli_connect($server_name, $mysql_user, $mysql_pass, $db_name);
if(!$con){
echo '{"message":"Unable to connect to the database."}';
}
?>
这就是我在android中的point.java
public class Point extends Activity implements View.OnClickListener{
String Err;
TextView err;
Button badd;
Context ctx=this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.point);
badd = (Button) findViewById(R.id.point_add);
err = (TextView) findViewById(R.id.err);
Err = getIntent().getStringExtra("err");
badd.setOnClickListener(this);
err.setText(Err);
}
public void onClick(View v){
BackGround b = new BackGround();
Intent intent = new Intent(this, Thanks.class);
startActivity(intent);
}
class BackGround extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String point = params[0];
String data="";
int tmp;
try {
URL url = new URL("http://192.168.1.12/android/point.php");
String urlParams = "point="+point;
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
OutputStream os = httpURLConnection.getOutputStream();
os.write(urlParams.getBytes());
os.flush();
os.close();
InputStream is = httpURLConnection.getInputStream();
while((tmp=is.read())!=-1){
data+= (char)tmp;
}
is.close();
httpURLConnection.disconnect();
return data;
} catch (MalformedURLException e) {
e.printStackTrace();
return "Exception: "+e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "Exception: "+e.getMessage();
}
}
@Override
protected void onPostExecute(String s) {
if(s.equals("")){
s="Data saved successfully.";
}
Toast.makeText(ctx, s, Toast.LENGTH_LONG).show();
}
}
公共类点扩展活动实现View.OnClickListener{
字符串错误;
文本视图错误;
按钮添加;
上下文ctx=此;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局点);
添加=(按钮)findViewById(R.id.point\u add);
err=(TextView)findViewById(R.id.err);
Err=getIntent().getStringExtra(“Err”);
添加.setOnClickListener(此);
err.setText(err);
}
公共void onClick(视图v){
背景b=新背景();
意图=新的意图(这个,谢谢,class);
星触觉(意向);
}
类后台扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串点=参数[0];
字符串数据=”;
int tmp;
试一试{
URL=新URL(“http://192.168.1.12/android/point.php");
字符串urlParams=“点=”+点;
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setDoOutput(true);
OutputStream os=httpURLConnection.getOutputStream();
write(urlParams.getBytes());
os.flush();
os.close();
InputStream=httpURLConnection.getInputStream();
而((tmp=is.read())!=-1){
数据+=(字符)tmp;
}
is.close();
httpURLConnection.disconnect();
返回数据;
}捕获(格式错误){
e、 printStackTrace();
return“Exception:+e.getMessage();
}捕获(IOE异常){
e、 printStackTrace();
return“Exception:+e.getMessage();
}
}
@凌驾
受保护的void onPostExecute(字符串s){
如果(s.等于(“”){
s=“数据已成功保存。”;
}
Toast.makeText(ctx,s,Toast.LENGTH_LONG).show();
}
}
}
没有错误,安卓系统工作正常。但我的观点没有更新为1。我的代码哪里出错了吗?point.php是实际的文件吗?因为那里有很多错误。您定义了3次变量$sql,前两次对代码没有任何影响。我认为这是一个被截断的变量,因为您使用了文件中未定义的变量,您能清理一下point.php吗?如果这实际上是整个文件,请尝试删除该错误报告(0)并处理错误。如果我理解正确,您希望在每次单击按钮时添加计数器并将其推入数据库。您可以使用sharedPref或simple来完成此操作,int counter=0, 点击按钮计数器一次+=1;
然后,您可以在数据库中推送计数器值,也可以尝试使用字符串:)亲爱的p.Rempe。我想我现在已经把point.php的代码弄好了。我应该这样写吗?但我仍然不知道如何在每次单击按钮时添加1。$sql=“UPDATE user SET point='$point',其中id=$id;”;您正在“搜索”id与$id匹配的条目,该变量未在任何地方声明。嗨,我想将id点上的字符串\u add-in和id更改为整数,以便将它们作为整数添加到点列中。我更新了密码。我应该这样做吗?我仍然无法将点添加到我的数据库中的列点,即使我认为我现在正确地编写了代码。。