|  | 
	                
 
 
  
    | 
        
        ดึงข้อมูลมาฝั่ง App ได้แล้ว แต่จะส่งข้อมูลกลับไปยัง Database ยังไงดีครับ     |  
    |  |  
 
              
  
    | 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ใช้วิธีการดึงข้อมูลมาอย่างไหร ก็ใช้ วิธีการเดียวกัน ส่งข้อมูลไปให้ Server ครับ เปลี่ยนแค่ชื่อตัวแปร และข้อมูลที่ส่ง เท่านัั้น
 
 Server จะเป็นตัวเลือก ตาม ตัวแปรที่ได้รับ ว่าจะ select  update หรือ  delete
 ตามแต่คุณจะเขียนครอบคลุมขนาดไหน
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2014-11-24 07:36:54 | By :
                            Chaidhanan |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ตอนส่งทีละรายการอาหารได้ครับ แต่จะส่งทั้งหมดในครั้งเดี่ยวได้ไหมครับ มันต้องใช้ลูปหรือเปล่าครับในการส่ง รายการทั้งหมด ผมพึงศึกษาเกี่ยวกับภาษานี้อ่าครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2014-11-24 15:44:33 | By :
                            pelangi123 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | รู้สึกจะตอบไปแล้วน่ะครับ ถ้าคุณอยากส่งหลาย ๆ รายการ จะต้องแปลงให้เป็น JSON ก่อนครับ แล้วค่อยส่งไป 
 
  
 Android Add Insert Save data to Server Database
 
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2014-11-24 17:24:32 | By :
                            mr.win |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ขอบคุณครับ แต่ผมทำ  app  บน iphone ครับ ที่ใช้ภาษา Objective-C พอดีพึงศึกษาภาษานี้ครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2014-11-24 22:23:14 | By :
                            pelangi123 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              |  ตอบความคิดเห็นที่ : 4 เขียนโดย : pelangi123 เมื่อวันที่ 2014-11-24 22:23:14 
 รายละเอียดของการตอบ ::
    กรณีการ SendData To Server กรณีนี้ผมใช้ MSSQL เป็น Database นะครับ(Objective c + PHP) วิธีการคือ  
- เเปลงข้อมูลในให้อยู่ในรูปเเบบ JSON ก่อน 
- สร้าง php ไฟล์ บน Webserver ไว้รับข้อมูลที่ส่งมาจาก Objective c เเล้วบันทึกลง database 
- ส่งค่าผลการบันทึกข้อมูลกับไปยัง objective c 
Code (Objective-C) 
- (IBAction)SendData:(id)sender
{
    NSArray *info = [NSArray arrayWithArray:myObject];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:info options:NSJSONWritingPrettyPrinted error:nil];  //เเปลงข้อมูลในให้อยู่ในรูปเเบบ JSON ก่อน
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSString *post = [NSString stringWithFormat:@"sStock=%@", jsonString];  //สร้างตัวเเปร sStock เก็บข้อมูลที่ต้องการส่ง
  
    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
  
    NSURL *url = [NSURL URLWithString:@"http://localhost/InsertStockVerify.php"];    //ส่งผ่าน url
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                           timeoutInterval:10.0];
                                                           [request setHTTPMethod:@"POST"];
       
     [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
     [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
     [request setHTTPBody:postData];
     NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
     // Show Progress Loading...
   
     [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
     loading = [[UIAlertView alloc] initWithTitle:@"" message:@"Please Wait..." delegate:nil cancelButtonTitle:nil                    otherButtonTitles:nil];
     UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
 
     progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
 
    [loading addSubview:progress];
    [progress startAnimating];
    [loading show];
        
     if (conn) //กรณี network ขัดข้อง
     {
      self.receivedResult = nil;
     }
      else
     { 
      UIAlertView *connectionFailMessage = [[UIAlertView alloc] initWithTitle:@"Failed Send Data to Server!!"                          
                                           message:@"เกิดความผิดพลาดขณะทำรายการ กรุณาทำรายการใหม่อีกครั้ง" delegate:self
                                           cancelButtonTitle:@"OK" otherButtonTitles : nil];
   
     [connectionFailMessage show];
     }
}
 ส่วน PHP บน Webserver
 code ประมาณนี้ครับ
 Code (PHP)
 
 <?php
      $serverName = "NONAMESERVER";
      $connectionInfo = array("Database"=>"DBandroid", "UID"=>"sa", "PWD"=>"PASSWORD");
      $conn = sqlsrv_connect($serverName, $connectionInfo);
     
      /* Begin the transaction. */ 
      sqlsrv_begin_transaction( $conn );
      $arr = null;
      $var = $_POST['sStock'];
      $jsonArray = json_decode($var, true);
          foreach ($jsonArray as $result) 
          {
	    $result["Procode"];
	    $result["Barcode"];
       	    $result["Size"];
	    $result["Colorcode"];
            $result["Desc"];
	    $result["Price"];
            $DateNow = date("Y-m-d H:i:s");
            $sql = "INSERT INTO stockverify (branch, stock_date, log_time, barcode, prodcode, size, color, price) VALUES (?,?,?,?,?,?,?,?)";
            $color = iconv('UTF-8', 'TIS-620',$result["Desc"]); //เเก้ปัญหาภาษาไทย
            $params = array('C001', date("Y-m-d"), date("H:i:s"), $result["Barcode"], $result["Procode"], $result["Size"], $color, $result["Price"]);
            $stmt = sqlsrv_query($conn,$sql,$params);   
          }
	
       /* If both queries were successful, commit the transaction.  */
       /* Otherwise, rollback the transaction. */
       if( $stmt ) 
       {
          sqlsrv_commit( $conn );
          $arr["Status"] = "1";
	  $arr["Message"] = "Send Data Successfully";  
       } 
        else 
       {
          sqlsrv_rollback( $conn );
	  $arr["Status"] = "0";
	  $arr["Message"] = "Send Data Failed";
       }
		 
      sqlsrv_free_stmt($stmt); //คืนค่าทรัพยากร
      echo json_encode($arr);
	
    sqlsrv_close($conn);	
?>
 |  
              | 
 ประวัติการแก้ไข
 2014-11-25 08:33:56
 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2014-11-25 08:32:50 | By :
                            junior_dev |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ขอบคุณครับ ถ้าจะให้เก็บในฐานข้อมูลหลายๆรายการในครั้งเดี่ยวที่ส่งจาก app  มันสามารถทำได้ไหมครับ ประมาณว่า app สั่งอาหารจะส่ง Order ทั้งหมด (อาหารทั้งหมด) ไปยัง Server ในครั้งเดี่ยวแล้วเก็บลงในฐานข้อมูล ได้ไหมครับ ขอบคุรครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2014-11-25 22:58:50 | By :
                            pelangi123 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              |  
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2014-11-25 23:26:09 | By :
                            pelangi123 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ประมาณนี้ครับบ  คือทุกรายการอาหารจะดึงจากฐานข้อมูลซึ่งแปลงเป็น json เรียบร้อยแล้ว หน้าที่ 3 เป๋นการส่งรายการไปเก็บในตะกร้า จากนั้นจะมีอีกหน้า เป็นหน้า order (ตอนนี้ยังไม่มี) จะดึงรายการอาหารทั้งหมดมาแสดง แล้วจะส่งรายการอาหารทั้งหมดกลับไปยังฐานข้อมูลทั้งหมด ในครั้งเดี่ยว มันจะส่งได้ไหมครับ ขอคุณครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2014-11-25 23:32:16 | By :
                            pelangi123 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ได้ ครับ แต่ ข้างบนเขาบอก ถึงขนาดนั้นแล้ว ไม่ลองทำดูก่อนล่ะครับ ถามอย่างเดียว ไม่ลองก็คงไปไหนไม่ได้หรอกครับ 
 และถ้าลองแล้ว ติดปัญหา ก็เอาโค๊ดที่ลองแล้ว ทำไม่ได้ มาโพสมาถามต่อได้นะครับ มีคนช่วยตอบเยอะครับ
 
 แต่ไม่เห็นความก้าวหน้าของสิ่งที่ตอบไป ก็หมดกำลังใจที่จะตอบนะครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2014-11-26 07:58:17 | By :
                            Chaidhanan |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ขอบคุณครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2014-11-26 15:24:05 | By :
                            pelangi123 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ในบทความก็มีครับ การส่งค่าจาก iOS ไปยัง PHP ครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2014-11-26 15:45:39 | By :
                            mr.win |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  |  |