Android应用程序不保存在mysql数据库中

时间:2014-11-29 21:50:17

标签: android mysql

我有预订活动,允许用户保留表格。问题是不能将任何东西保存到mysql数据库中。在LogCat中没有显示任何内容,也没有任何错误。只需重新加载相同的活动。活动流程是

Activity_1 -> Activity_2 -> Activity_3 -> Activity_4 -> Activity_5 (last and finish)

我已经通过活动之间的意图传递了我需要的数据。

Activity_2

Intent newActivity = new Intent(Activity_2.this, Activity_3.class); 
    newActivity.putExtra("Position", Position); 
    newActivity.putExtra("resultServer", resultServer); 
    newActivity.putExtra("id", MyArrList.get(position).get("id").toString());
    startActivity(newActivity); 

Activity_3

Intent intent= getIntent();
    table_id = intent.getStringExtra("id");
btnMenues.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
                Intent intent = new Intent(Activity_3.this, Activity_4.class);
                String Name = editText1.getText().toString();
                String Email = editText2.getText().toString();
                String Phone = editText3.getText().toString();
                String Comment = editText4.getText().toString();
                String DateTime = datePicker.getText().toString();
                String numberOfPeople = editText5.getText().toString();

                intent.putExtra("Name", Name);
                intent.putExtra("Email", Email);
                intent.putExtra("Phone", Phone);
                intent.putExtra("Comment", Comment);
                intent.putExtra("DateTime", DateTime);
                intent.putExtra("numberOfPeople", numberOfPeople);
                intent.putExtra("table_id", table_id);

                startActivity(intent);
            }
        //}
    }); 

}

Activity_4

 Bundle extras = getIntent().getExtras();
    if (extras != null) {
        getName = extras.getString("Name");
        getEmail = extras.getString("Email");
        getPhone = extras.getString("Phone");
        getComment = extras.getString("Comment");
        getDateTime = extras.getString("DateTime");
        getnumberOfPeople = extras.getString("numberOfPeople");
        table_id = extras.getString("table_id");
    }
 Intent newActivity = new Intent(Activity_4.this, Activity_5.class);
    newActivity.putExtra("Position", Position);
    newActivity.putExtra("resultServer", resultServer);
    newActivity.putExtra("table_id", MyArrList.get(position).get("table_id").toString());

            startActivity(newActivity);

Activity_5

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.table_information);

    Intent intent= getIntent();
    curPosition = Integer.parseInt(intent.getStringExtra("Position")); 
    resultServer = String.valueOf(intent.getStringExtra("resultServer")); 

    try {
        MyArrList = ConvertJSONtoArrayList(resultServer);

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Log.d("ArrayList Size",String.valueOf(MyArrList.size()));

    // Show Image Full
    new DownloadFullPhotoFileAsync().execute();

    btnFinish = (Button) findViewById(R.id.btnFinish);

    btnFinish.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Bundle extras = getIntent().getExtras();
            if (extras != null) {
                getName = extras.getString("Name");
                getEmail = extras.getString("Email");
                getPhone = extras.getString("Phone");
                getComment = extras.getString("Comment");
                getDateTime = extras.getString("DateTime");
                getnumberOfPeople = extras.getString("numberOfPeople");
                getTable = extras.getString("table_id");
        }

            new SummaryAsyncTask().execute((Void) null);

            startActivity(getIntent());
        }
    });

}
class SummaryAsyncTask extends AsyncTask<Void, Void, Boolean> {

    private void postData(String getNameToData, String getEmailData, String getPhoneData,
            String getCommentData, String getDateTimeData, String getnumberOfPeopleData, String getTable) {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://link.to.site/save.php");

        try {
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
            nameValuePairs.add(new BasicNameValuePair("Name", getNameToData));
            nameValuePairs.add(new BasicNameValuePair("Email", getEmailData));
            nameValuePairs.add(new BasicNameValuePair("Phone", getPhoneData));
            nameValuePairs.add(new BasicNameValuePair("Comment", getCommentData));
            nameValuePairs.add(new BasicNameValuePair("Date", getDateTimeData));
            nameValuePairs.add(new BasicNameValuePair("numberOfPeople", getnumberOfPeopleData));
            nameValuePairs.add(new BasicNameValuePair("table_id", getTable));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
        }
        catch(Exception e)
        {
            Log.e("log_tag", "Error:  "+e.toString());
        }
    }

我错过了某个地方,但却无法弄明白究竟是什么。

使用save.php

进行更新
$icon = mysql_connect("localhost","user","pass");
if(!$icon)
{ 
   die('Could not connect : ' . mysql_erroe());
}
mysql_select_db("database", $icon)or die("database selection error");
echo json_encode($data);

$Name=$_POST['Name'];
$Phone=$_POST['Phone'];
$Email=$_POST['Email'];
$Comment=$_POST['Comment'];
$DateTime=$_POST['DateTime'];
$numberOfPeople=$_POST['numberOfPeople'];
    $DateTime= date("Y-m-d H:i:s", strtotime($DateTime));
    $table_id = $_POST['table_id'];

mysql_query("INSERT INTO reservation (Name, Phone, Email, Comment, DateTime, numberOfPeople, table)
         VALUES ('$Name', '$Phone', '$Email', '$Comment', '$DateTime', '$numberOfPeople', '$table_id')",$icon);

mysql_close($icon);

logcatActivity_3意图丢失之后更新。

11-30 03:40:59.497: D/Activity_3(1642): testName test@test.com 123456 testComment 30-11-2014 3:40 2 1
11-30 03:41:01.817: D/Activity_4(1642): null null null null null null 1
11-30 03:41:04.667: D/Actiovity_5(1642): org.apache.http.message.BasicHttpResponse@b2f5c9a0

Activity_4 {仅保留table_id

1 个答案:

答案 0 :(得分:1)

您的代码不完整,您只编写了快乐案例。

您缺少以下内容:

  • echo json_encode($data);数据未定义
  • POST值服务器端检查(使用isset())
  • 升级到mysqli_或至少使用mysql_real_escape()
  • BasicNameValuePair("Date", getDateTimeData)$DateTime=$_POST['DateTime'];不匹配
  • 检查查询是否正确执行
  • 检查是否已插入行
  • 在PHP中添加错误报告
  • 使用日志记录(例如error_log())或使用网络工具来查看请求
  • 使用LogCat
  • 在java中记录响应

if(isset($_POST['Name'], $_POST['Phone'], $_POST['Email'], $_POST['Comment'], 
        $_POST['numberOfPeople'], $_POST['DateTime'])){

    $icon = mysql_connect("localhost","user","pass", "database");
    if(!$icon)
    { 
       die('Could not connect : ' . mysql_error());
    }

    $name=$_POST['Name'];
    $phone=$_POST['Phone'];
    $email=$_POST['Email'];
    $comment=$_POST['Comment'];
    $number_of_people=$_POST['numberOfPeople'];
    $date_time= date("Y-m-d H:i:s", strtotime($_POST['DateTime']));
    $table_id = $_POST['table_id'];



    $query = sprintf("
             INSERT INTO reservation 
             (`Name`, `Phone`, `Email`, `Comment`, 
              `DateTime`, `numberOfPeople`, `table`)
             VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s')",
                mysql_real_escape_string($name),
                mysql_real_escape_string($phone),                       
                mysql_real_escape_string($email)),
                mysql_real_escape_string($comment),
                mysql_real_escape_string($date_time),
                mysql_real_escape_string($number_of_people),
                mysql_real_escape_string($table_id);


    $result = mysql_query($query,$icon);

    if(!$result)
    {
        die('Could not execute : ' . mysql_error());
    }

    if(mysql_num_rows($result) > 0){
        echo "Success !";
    }else{
        echo "Fail !";
    }

    mysql_close($icon);
}else{
    echo "missing values";
}

修改

不要忘记在android中记录响应:

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Log.d(LOG_TAG, response.toString());

再次修改 如果您在activity4中获得空值,则表示Activity3中的editText为空,请确保它们在onCreate()中初始化

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editText1 =  (EditText) findViewByID(R.id.editText1);
    editText2 =  (EditText) findViewByID(R.id.editText2);
    editText3 =  (EditText) findViewByID(R.id.editText3);
    ......
}

然后在您的按钮事件上执行此操作:

@Override
public void onClick(View v) {
        Intent intent = new Intent(Activity_3.this, Activity_4.class);
        intent.putExtra("Name", editText1.getText());
        intent.putExtra("Email", editText2.getText());
        intent.putExtra("Phone", editText3.getText());
        intent.putExtra("Comment", editText4.getText());
        intent.putExtra("DateTime", datePicker.getText());
        intent.putExtra("numberOfPeople", editText5.getText());
        intent.putExtra("table_id", getIntent().getStringExtra("id"));
        startActivity(intent);
}