2014-11-08 49 views
0

我有一個保留表格,它必須保存信息到MySQL數據庫。在第一個Axtivity用戶填寫表格,然後惠特意圖我保存的信息和passit第二個活動,其中是按鈕保存。當我點擊按鈕保存什麼都不去數據庫。 這是活動1活動不會保存信息到MySQL數據庫

static String Name; 
static String Email; 
static String Phone; 
static String Comment; 
static String DateTime; 
static String numberOfPeople; 

private EditText editText1, editText3, editText2, editText4, datePicker, editText5; //, txtTime; 
private Button btnMenues, btnTimePicker, btnCalendar; 
CustomDateTimePicker custom; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.reservation); 
    custom = new CustomDateTimePicker(this, 
      new CustomDateTimePicker.ICustomDateTimeListener() { 

       @Override 
       public void onSet(Dialog dialog, Calendar calendarSelected, 
         Date dateSelected, int year, String monthFullName, 
         String monthShortName, int monthNumber, int date, 
         String weekDayFullName, String weekDayShortName, 
         int hour24, int hour12, int min, int sec, 
         String AM_PM) { 
        ((EditText) findViewById(R.id.datePicker)).setText(calendarSelected.get(Calendar.DAY_OF_MONTH) 
            + "/" + (monthNumber+1) + "/" + year 
            + ", " + hour12 + ":" + min 
            + " " + AM_PM); 
       } 

       @Override 
       public void onCancel() { 

       } 
      }); 
    /** 
    * Pass Directly current time format it will return AM and PM if you set 
    * false 
    */ 
    custom.set24HourFormat(false); 
    /** 
    * Pass Directly current data and time to show when it pop up 
    */ 
    custom.setDate(Calendar.getInstance()); 

    findViewById(R.id.btnCalendar).setOnClickListener(
      new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        custom.showDialog(); 
       } 
});   

    editText1 = (EditText) findViewById(R.id.personName); 
    editText3 = (EditText) findViewById(R.id.personPhone); 
    editText2 = (EditText) findViewById(R.id.personEmail); 
    editText4 = (EditText) findViewById(R.id.personComment); 
    datePicker = (EditText) findViewById(R.id.datePicker); 
    editText5 = (EditText) findViewById(R.id.editText5); 

    btnCalendar = (Button) findViewById(R.id.btnCalendar); 
    btnMenues = (Button) findViewById(R.id.continueWithReservation); 

    btnMenues.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      Intent intent = new Intent(Reservation.this, ReservationSecond.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); 

      startActivity(intent); 
     } 
    }); 
} 

這是活動2

String getName; 
String getEmail; 
String getPhone; 
String getComment; 
String getDateTime; 
String getnumberOfPeople; 

private Button btnMenues; 

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

    btnMenues = (Button) findViewById(R.id.finish); 

    btnMenues.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"); 
       Log.e("err", getName + " " + getEmail + " " + getPhone + " " + getComment + " " + getDateTime + " " + getnumberOfPeople); 
      } 

      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) { 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://link.to.site/saveReservation.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("DateTime", getDateTimeData)); 
      nameValuePairs.add(new BasicNameValuePair("numberOfPeople", getnumberOfPeopleData)); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient.execute(httppost); 
     } 
     catch(Exception e) 
     { 
      Log.e("log_tag", "Error: "+e.toString()); 
     } 
    } 

    @Override 
    protected Boolean doInBackground(Void... params) { 
     postData(getName, getEmail, getPhone, getComment, getDateTime, getnumberOfPeople); 
     return null; 
    } 

} 

我已經把第二個活動的意圖部分日誌檢查Log.e("err", getName + " " + getEmail + " " + getPhone + " " + getComment + " " + getDateTime + " " + getnumberOfPeople);LogCat我看到這個錯誤時按鈕保存到數據庫點擊

11-08 17:04:44.654: E/err(1112): Test [email protected] 33733721 Hello 26/11/2014, 4:50 PM 3 

這是第一個活動

XML
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

     <EditText 
      android:id="@+id/personName" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:ems="10" 
      android:hint="@string/NameForReservation" 
      android:inputType="textPersonName" 
      android:maxLength="25" />  

     <EditText 
      android:id="@+id/personPhone" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:ems="10" 
      android:hint="@string/ContactPhone" 
      android:inputType="phone" 
      android:maxLength="16" /> 

     <EditText 
      android:id="@+id/personEmail" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:ems="10" 
      android:hint="@string/CustomerEmailContact" 
      android:inputType="textEmailAddress" 
      android:maxLength="50" /> 

     <EditText 
      android:id="@+id/personComment" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:ems="10" 
      android:hint="@string/CustomerCommentar" 
      android:maxLength="100" /> 


    <LinearLayout 
     android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/linearLayout1" > 
    <Button 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:id="@+id/btnCalendar" 
     android:text="@string/ReservationDate" 
     android:layout_width="100dp" /> 
     <requestFocus></requestFocus> 
    <EditText 
     android:id="@+id/datePicker" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="0.27" 
     android:inputType="datetime" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/numberOfPeople" 
     android:layout_width="190dp" 
     android:layout_height="36dp" 
     android:layout_weight="0.04" 
     android:layout_marginLeft="10dp" 
     android:text="@string/numberOfPeople" />  
     <requestFocus></requestFocus> 

    <EditText 
     android:id="@+id/editText5" 
     android:layout_width="10dp" 
     android:layout_height="41dp" 
     android:layout_weight="0.02" 
     android:ems="10" 
     android:inputType="number" 
     android:maxLength="2" 
     android:text="" /> 

</LinearLayout> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
<Button 
    android:id="@+id/continueWithReservation" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="right" 
    android:gravity="center" 
    android:text="@string/reservationContinue" /> 
</LinearLayout> 
</LinearLayout> 

任何想法我在這裏想念什麼?

UPDATE從服務器端php文件

<?php 
$icon = mysql_connect("localhost","user","pass"); 
if(!$icon) 
{ 
    die('Could not connect : ' . mysql_erroe()); 
} 
mysql_select_db("db", $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']; 
date_default_timezone_set("Europe/Bucharest"); // time zone 
    $DateTime= date("Y-m-d H:i:s"); 

mysql_query("INSERT INTO reservation (Name, Phone, Email, Comment, DateTime, numberOfPeople) 
     VALUES ('$Name', '$Phone', '$Email', '$Comment', '$DateTime', '$numberOfPeople')",$icon); 
mysql_close($icon); 
+0

您可以登錄什麼是傳遞到你的PHP文件,並檢查你的Apache日誌,看看是否anaything是錯以上? – marshallino16 2014-11-08 23:53:22

+0

你得到的HttpResponse是什麼?這是嚴格在你有的服務器上完成的,因爲就我所見,你的Android代碼沒有任何錯誤。 – mobilepotato7 2014-11-08 23:59:21

+0

@goro你爲什麼要花這麼多時間來回答 – 2014-11-09 00:04:32

回答

1

你已經錯過了單引號 '$日期時間'

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

的mysql_query不贊成不使用它在新的項目。嘗試mysqli_或PDO

而且在你的PHP把它查詢

  date_default_timezone_set("Asia/Calcutta"); // choose your time zone 
     $DateTime= date("Y-m-d H:i:s"); 
+0

那麼謝謝你。現在至少在數據庫中保存了某些東西,但爲什麼保存的日期時間是'0000-00-00 00:00:00'? – Goro 2014-11-09 07:59:11

+0

如果我將DB中的字段'DateTime'更改爲'timestamp'是相同的。 – Goro 2014-11-09 08:00:31

+0

原因是您在約會中使用了逗號。更多的應該是在2014-11-31 22:00:00格式 – 2014-11-09 08:00:37