使用mysql和php更新列变量
我正在尝试更新现有数据库中名为Attention的列。基本上,我需要通过android应用程序在服务器上托管的数据库中增加出席人数列。下面是使用Async的代码,但它在onPostExecute处返回nullpointer异常使用mysql和php更新列变量,php,android,mysql,Php,Android,Mysql,我正在尝试更新现有数据库中名为Attention的列。基本上,我需要通过android应用程序在服务器上托管的数据库中增加出席人数列。下面是使用Async的代码,但它在onPostExecute处返回nullpointer异常 package com.example.soumya.attendance; public class RecordData extends Activity{ Button btnStartProgress; ProgressDialog progressBar
package com.example.soumya.attendance;
public class RecordData extends Activity{
Button btnStartProgress;
ProgressDialog progressBar;
Double lati;
Double longi;
// private ListView lvItem;
//private ArrayList<String> itemArrey;
//private ArrayAdapter<String> itemAdapter;
int count = 0;
int attendance_count = 0;
TextView textView;
Boolean attendance = false;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();
GPSTracker gps;
private long fileSize = 0;
// Create the Handler object (on the main thread by default)
Handler handler = new Handler();
String final_msg="";
Handler progressHandler = new Handler();
// Define the code block to be executed
private Handler mHandler = new Handler();
private Runnable runnableCode = new Runnable() {
@Override
public void run() {
// Do something here on the main thread
if(count<=6)
{Toast.makeText(getApplicationContext(), getDateTime(),
Toast.LENGTH_LONG).show();}
count++;
Log.d("Handlers", "Called on main thread");
// Repeat this the same runnable code block again another 2 seconds
//handler.postDelayed(runnableCode, (1 * 60) * 1000);
handler.postDelayed(runnableCode, 10 * 1000);
}
};
public void Logout()
{
Intent intent = new Intent(this, MainActivity.class);
this.startActivity(intent);
super.onDestroy();
}
public void update()
{
final String update_url = "http://172.16.92.250/update.php";
final String attendance = "1";
final String uname = "aneesha";
class UserLoginClass extends AsyncTask<String,Void,String>
{
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(RecordData.this, "Please Wait", null, true, true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
if (s.contains("success")) {
Intent intent = new Intent(RecordData.this, NewActivity.class);
//intent.putExtra(USER_NAME,username);
startActivity(intent);
} else {
Toast.makeText(RecordData.this, s, Toast.LENGTH_LONG).show();
}
}
@Override
protected String doInBackground(String... params)
{
try {
URL url = new URL(update_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String data = URLEncoder.encode("attendance", "UTF-8") + "=" + URLEncoder.encode(attendance, "UTF-8") + "&" +
URLEncoder.encode("uname", "UTF-8") + "=" + URLEncoder.encode(uname, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String response = "";
String line = "";
while ((line = bufferedReader.readLine())!=null)
{
response+= line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return response;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
UserLoginClass ulc = new UserLoginClass();
ulc.execute(attendance, uname);
}
// Intent intent = new Intent(this, NewActivity.class);
// this.startActivity(intent);
public void receiveMyMessage() {
Log.d("Textview", "found");
final TextView txt = (TextView)findViewById(R.id.username3);
Log.d("Textview", "Not found");
mHandler.post(new Runnable() {
@Override
public void run() {
// This gets executed on the UI thread so it can safely modify Views
if (attendance_count == count) {
txt.setText(final_msg+"\n\nYour attendance is marked!\nPlease logout");
txt.setTextColor(getResources().getColor(R.color.Green));
// BackgroundTask backgroundTask = new BackgroundTask();
// backgroundTask.execute("update","1");
Button btn = (Button) findViewById(R.id.update);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
update();
}
});
} else {
txt.setText(final_msg+"\n\nSorry, marked absent.\nPlease logout");
txt.setTextColor(getResources().getColor(R.color.Red));
// txt.setTextColor(getResources().getColor(Color.RED));
}
Button btn = (Button) findViewById(R.id.Button3);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Logout();
}
});
}
}); }
package com.example.soumya.attention;
公共类RecordData扩展活动{
按钮btnStartProgress;
ProgressDialog progressBar;
双纬度;
双纵;
//私有ListView项目;
//私人ArrayList itemArrey;
//专用ArrayAdapter项适配器;
整数计数=0;
int出席人数=0;
文本视图文本视图;
布尔值=false;
private int progressBarStatus=0;
私有处理程序progressBarHandler=新处理程序();
全球定位系统;
私有长文件大小=0;
//创建处理程序对象(默认情况下在主线程上)
Handler=newhandler();
字符串final_msg=“”;
Handler progressHandler=新处理程序();
//定义要执行的代码块
私有处理程序mHandler=新处理程序();
private Runnable runnableCode=new Runnable(){
@凌驾
公开募捐{
//在主线程上执行一些操作
如果(count首先,您必须更正您的PHP。您正在使用
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
及
mysql\u connect函数,然后尝试使用mysqli\u查询
msqyl_*和mysqli_*不兼容,不能同时使用,您可以使用:
- mysql\u连接
- mysql\u查询
- mysql\u num\u行
- mysql_fetch_assoc
或者您使用mysqli:
- mysqli_连接
- mysqli_查询
- mysqli_num_行
- mysqli_fetch_assoc
此外,结果变量assign中缺少$
从注释中,您还可以更改分配给此的$query:
$query = "UPDATE students SET attendance = ($attendance + attendance) WHERE uname = '$uname'");
并将$POST更改为$U POST
希望这有帮助。是result=mysqli\u查询($con,$query);
result
没有$
,像这样:$result=mysqli\u查询($con,$query)
?与$POST
相同。应该是$\u POST
在update.php
文件中。阅读超全局变量及其语法。顺便说一句,您的代码包含许多语法错误。另一个错误是其中uname=$uname”)
不带引号的字符串值。然后,您将在中使用mysql\u
引入mysqli\u
API。mysql\u*函数的可能副本已经被弃用了5年多,并且在当前的PHP版本中不存在。您不应该建议任何人使用它们!我不建议他使用它,我只是指出它们不可能是mi我不知道使用的是什么环境,可能是某个旧服务器需要更新,无法迁移。你上面的回答是“你要么使用。。。“然后列出mysql_*函数,建议使用它们。mysqli和PDO自2005年PHP 5.1发布以来就已经可用,因此没有理由不使用它们。是的,您可以使用其中一个,但不能同时使用两个,当我说“我建议使用…”时,通常我会以“PDO”结束这句话:
result = mysqli_query($con,$query);
$query = "UPDATE students SET attendance = ($attendance + attendance) WHERE uname = '$uname'");