2017-03-09 46 views
0

我正在開發一個Android應用程序,該應用程序通過PHP腳本將數據從應用程序發送到在線MYSQL數據庫。該應用程序用於記錄用戶的輸入值。我也有一個checkBox,用於檢查用戶是否缺席,並且沒有任何記錄要填寫。數據庫表中有一個名爲「ABSENT」的列,當用戶點擊時它會被標記爲「NO」輸入數據後提交。如果單擊複選框並且iput字段的值將填充0,那麼「ABSENT」列將被標記爲「YES」。所以,我想要做的是,如果用戶輸入值並單擊「Submit Button」(提交按鈕),我調用AsyncTask並將每個輸入字段的值與字符串值一起傳遞給「ABSENT」列。根據複選框填充MySQL表格通過PHP腳本在Android中單擊

String type = "insertDetails"; 
String absent = "No"; 
BackgroundAsyncTask asyncTask = new BackgroundAsyncTask(InsertDataActivity.this); 
asyncTask.execute(type,enquiryValue,retailValue,collectionValue, 
        bookingValue,evaluationValue,testDriveValue,homeVisitValue, 
          username,dateValue,absent); 

複選框代碼是:

boolean checked = ((CheckBox) v).isChecked(); 
if (checked) { 
Toast.makeText(getApplicationContext(),"You are marked as absent for the Day.",Toast.LENGTH_SHORT).show(); 
String type = "insertDetails"; 
String absent = "Yes"; 
BackgroundAsyncTask asyncTask = new BackgroundAsyncTask(InsertDataActivity.this); 
asyncTask.execute(type,enquiryValue,retailValue,collectionValue, bookingValue,evaluationValue,testDriveValue,homeVisitValue, 
          username,dateValue,absent); 

的的AsyncTask代碼:

if (type.equals("insertDetails")){ 
      try { 
       String enquiry = params[1]; 
       String retail = params[2]; 
       String collection = params[3]; 
       String booking = params[4]; 
       String evaluation = params[5]; 
       String test_drive = params[6]; 
       String home_visit = params[7]; 
       String user_name = params[8]; 
       String dateTime = params[9]; 
       String absent = params[10]; 

       URL url = new URL(insertData_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 post_data = URLEncoder.encode("enquiry", "UTF-8") + "=" + URLEncoder.encode(enquiry,"UTF-8") + "&" 
         + URLEncoder.encode("retail","UTF-8") + "="+ URLEncoder.encode(retail,"UTF-8") + "&" 
         + URLEncoder.encode("collection", "UTF-8") + "=" + URLEncoder.encode(collection,"UTF-8") + "&" 
         + URLEncoder.encode("booking", "UTF-8") + "=" + URLEncoder.encode(booking,"UTF-8") + "&" 
         + URLEncoder.encode("evaluation", "UTF-8") + "=" + URLEncoder.encode(evaluation,"UTF-8") + "&" 
         + URLEncoder.encode("test_drive", "UTF-8") + "=" + URLEncoder.encode(test_drive,"UTF-8") + "&" 
         + URLEncoder.encode("home_visit", "UTF-8") + "=" + URLEncoder.encode(home_visit,"UTF-8") + "&" 
         + URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name,"UTF-8") + "&" 
         + URLEncoder.encode("date", "UTF-8") + "=" + URLEncoder.encode(dateTime,"UTF-8") 
         +URLEncoder.encode("absent","UTF-8") + "=" + URLEncoder.encode(absent,"UTF-8"); 

而且PHP腳本將數據發送到MySQL是:

$enquiry = $_POST["enquiry"]; 
    $retail = $_POST["retail"]; 
    $collection = $_POST["collection"]; 
    $booking = $_POST["booking"]; 
    $evaluation = $_POST["evaluation"]; 
    $test_drive = $_POST["test_drive"]; 
    $home_visit = $_POST["home_visit"]; 
    $user_name = $_POST["user_name"]; 
    $update_date = $_POST["date"]; 
    $absent = $_POST["absent"]; 

    $mysql_qry1 = "INSERT INTO employee_details(enquiry,retail, 
    collection,booking, evaluation, test_drive, home_visit, name, date,time,absent) values ('$enquiry','$retail','$collection','$booking','$evaluation','$test_drive', 
    '$home_visit','$user_name','$update_date','$time','$absent');"; 
$conn->query($mysql_qry1); 

但這不會產生所需的結果當我嘗試插入數據時,「缺省」列爲空。任何人都可以幫助我嗎?

+1

老兄,有很多代碼重複 –

回答

2

您在post_data初始化代碼在這裏的第一行的末尾忘記+ "&"

+ URLEncoder.encode("date", "UTF-8") + "=" + URLEncoder.encode(dateTime,"UTF-8") 
+ URLEncoder.encode("absent","UTF-8") + "=" + URLEncoder.encode(absent,"UTF-8"); 

在結果你會得到奇怪的日期,而不能缺席參數。只需添加&即可。

+0

OMG,這是我犯的愚蠢的錯誤。謝謝了很多。你救了我的一天。再次感謝 :) – Pranami

相關問題