我正在尝试将数据从Android应用程序发送到MySQL数据库,它工作正常,但只有一个用户名字段无效。基本上,应用程序将问题数据发送到DB,只有一个用户名字段保持为空,其他工作正常。
以下是代码: 这是我尝试发送的MainActivity
package miniandroid.com.activities;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.NotificationManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import miniandroid.com.R;
public class MainActivity extends Activity {
private TextView ques3s1;
private EditText edit3s1, edit1, edit2, edit4;
private RadioGroup radioSexGroup;
private RadioButton radioSexButton1, radioSexButton3;
private ProgressDialog progress;
private Dialog dialog;
private static final int MY_NOTIFICATION_ID = 11;
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification);
mNotificationManager = (NotificationManager) getSystemService(ns);
mNotificationManager.cancel(MY_NOTIFICATION_ID);
radioSexButton1 = (RadioButton) findViewById(R.id.radioButton);
radioSexButton3 = (RadioButton) findViewById(R.id.radioButton3);
edit1 = (EditText) findViewById(R.id.rg_qus1);
edit2 = (EditText) findViewById(R.id.rg_qus2);
edit4 = (EditText) findViewById(R.id.rg_qus4);
ques3s1 = (TextView) findViewById(R.id.qus3s1);
edit3s1 = (EditText) findViewById(R.id.rg_qus3s1);
ques3s1.setVisibility(View.GONE);
edit3s1.setVisibility(View.GONE);
SharedPreferences prefs = getSharedPreferences("emood_veriif", MODE_PRIVATE);
String user = prefs.getString("Username", "");
setTitle("sMood " + user);
progress = new ProgressDialog(this);
progress.setMessage("Loading...");
dialog = new AlertDialog.Builder(this).create();
radioSexGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioSexGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
SharedPreferences prefs = getSharedPreferences("emood_veriif", MODE_PRIVATE);
String question3s1 = prefs.getString("Question3s1", "");
String question3s2 = prefs.getString("Question3s2", "");
if (radioSexButton1.isChecked()) {
ques3s1.setText(question3s1);
ques3s1.setVisibility(View.VISIBLE);
edit3s1.setVisibility(View.VISIBLE);
} else if (radioSexButton3.isChecked()) {
ques3s1.setText(question3s2);
ques3s1.setVisibility(View.VISIBLE);
edit3s1.setVisibility(View.VISIBLE);
} else {
ques3s1.setVisibility(View.GONE);
edit3s1.setVisibility(View.GONE);
}
}
});
Button continues = (Button) findViewById(R.id.btn_contsubmit);
continues.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences prefs = getSharedPreferences("emood_veriif", MODE_PRIVATE);
String question1 = prefs.getString("Question1", "");
String question2 = prefs.getString("Question2", "");
String question3 = prefs.getString("Question3", "");
String question4 = prefs.getString("Question4", "");
String question3s1 = prefs.getString("Question3s1", "");
String question3s2 = prefs.getString("Question3s2", "");
String user = prefs.getString("Username", "");
String ans1 = edit1.getText().toString();
String ans2 = edit2.getText().toString();
String ans3 = edit3s1.getText().toString();
String ans4 = edit4.getText().toString();
String radioanswer;
String question3s;
if (radioSexButton1.isChecked()) {
radioanswer = "Happy";
question3s = question3s1;
} else if (radioSexButton3.isChecked()) {
radioanswer = "Sad";
question3s = question3s2;
} else {
radioanswer = "Normal";
question3s = "";
}
PostAnswerTask task = new PostAnswerTask();
task.execute(user, question1, ans1, question2, ans2, question3, radioanswer, question3s, ans3, question4, ans4);
}
});
}
class PostAnswerTask extends AsyncTask<String, Void, String> {
protected void onPreExecute() {
progress.show();
}
@Override
protected String doInBackground(String... arg0) {
try {
String user = arg0[0];
String question1 = arg0[1];
String ans1 = arg0[2];
String question2 = arg0[3];
String ans2 = arg0[4];
String question3 = arg0[5];
String radioanswer = arg0[6];
String question3s = arg0[7];
String ans3 = arg0[8];
String question4 = arg0[9];
String ans4 = arg0[10];
String link = "http://miniandroid.com/answered.php";
String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(user, "UTF-8");
data += "&" + URLEncoder.encode("question1", "UTF-8") + "=" + URLEncoder.encode(question1, "UTF-8");
data += "&" + URLEncoder.encode("ans1", "UTF-8") + "=" + URLEncoder.encode(ans1, "UTF-8");
data += "&" + URLEncoder.encode("question2", "UTF-8") + "=" + URLEncoder.encode(question2, "UTF-8");
data += "&" + URLEncoder.encode("ans2", "UTF-8") + "=" + URLEncoder.encode(ans2, "UTF-8");
data += "&" + URLEncoder.encode("question3", "UTF-8") + "=" + URLEncoder.encode(question3, "UTF-8");
data += "&" + URLEncoder.encode("radioanswer", "UTF-8") + "=" + URLEncoder.encode(radioanswer, "UTF-8");
data += "&" + URLEncoder.encode("question3s", "UTF-8") + "=" + URLEncoder.encode(question3s, "UTF-8");
data += "&" + URLEncoder.encode("ans3", "UTF-8") + "=" + URLEncoder.encode(ans3, "UTF-8");
data += "&" + URLEncoder.encode("question4", "UTF-8") + "=" + URLEncoder.encode(question4, "UTF-8");
data += "&" + URLEncoder.encode("ans4", "UTF-8") + "=" + URLEncoder.encode(ans4, "UTF-8");
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter
(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while ((line = reader.readLine()) != null) {
sb.append(line);
break;
}
return sb.toString();
} catch (Exception e) {
return new String("Unable to connect. Please contact your network provider.");
}
}
@Override
protected void onPostExecute(String result) {
if (result.equals("Done")) {
progress.dismiss();
Intent intent = new Intent(MainActivity.this, AnsweredActivity.class);
startActivity(intent);
finish();
} else {
progress.dismiss();
((AlertDialog) dialog).setMessage(result);
dialog.show();
}
}
}
}
这是托管的php文件。
<?php
$db=new mysqli('fdb12.freehostingeu.com',
'2099457_app',
'kimPossible123',
'2099457_app');
$stmt=$db->stmt_init();
$user=$_POST['user'];
$question1=$_POST['question1'];
$ans1=$_POST['ans1'];
$question2=$_POST['question2'];
$ans2=$_POST['ans2'];
$question3=$_POST['question3'];
$radioanswer=$_POST['radioanswer'];
$question3s=$_POST['question3s'];
$ans3=$_POST['ans3'];
$question4=$_POST['question4'];
$ans4=$_POST['ans4'];
if($stmt->prepare("INSERT INTO question_answer
(`Username`,`Question1`,`Answer1`,
`Question2`,`Answer2`,
`Question3`,`Answer3`,
`Question3_Sub`,`Answer3_Sub`,
`Question4`,`Answer4`)
VALUES (?,?,?,?,?,?,?,?,?,?,?)")) {
$stmt->bind_param('sssssssssss',
$user,$question1,$ans1,
$question2,$ans2,
$question3,$radioanswer,
$question3s,$ans3,
$question4,$ans4);
$result=$stmt->execute();
}
if ($result) {
echo "Done";
} else{
echo "An Error Occured";
}
?>
在我的注册文件中,一切正常.db为所有字段设置了varchar数据类型..我似乎无法弄清楚问题是什么。该指数看起来很好,而且问题正在进入正确的领域。
非常感谢任何帮助。 此致
答案 0 :(得分:0)
请注意,我会在插入变量之前对变量使用mysqli_real_escape_string,以防止sql注入和查询错误。
$user=$db->real_escape_string($_POST['user']);
$question1=$db->real_escape_string($_POST['question1']);
$ans1=$db->real_escape_string($_POST['ans1']);
$question2=$db->real_escape_string($_POST['question2']);
$ans2=$db->real_escape_string($_POST['ans2']);
$question3=$db->real_escape_string($_POST['question3']);
$radioanswer=$db->real_escape_string($_POST['radioanswer']);
$question3s=$db->real_escape_string($_POST['question3s']);
$ans3=$db->real_escape_string($_POST['ans3']);
$question4=$db->real_escape_string($_POST['question4']);
$ans4=$db->real_escape_string($_POST['ans4']);
// then echo out the $user to see if it was passed
echo $user;
然后我会尝试打印用户名以查看它是否正确传递给脚本。如果没有打印,则错误在MainActivity代码中的某处。可能是在准备这个职位的时候。