2015-05-07 68 views
0

我正在爲我的客戶端編碼使用以下代碼。它不按我想要的方式工作,也不會將數據保存到遠程服務器。如果我將鏈接直接傳遞給Web瀏覽器,那麼它會將數據保存到遠程數據庫。無法將數據插入遠程數據庫

客戶端:

-(IBAction)Save:(id)sender { 
     NSString *insrt=[NSString stringWithFormat:@"http://localhost/basInsertv1.php?Zipcode=%i&Locality=%@&Propertytype=%@&pricerange=%@&Bedrooms=%i&Bathrooms=%i&Possession=%@&Propertyage=%@&Rating=%@&Parking=%i&Powerbackup=%i&Security=%i&Swimmingpool=%i",zipcodetf.text.intValue,localitytf.text,proprtytypetf.text,sliderresltlabel.text,bedroomstf.text.intValue,bathrooms.text.intValue,possesiontf.text,propertyAge.text,rating.text,parking.text.intValue,pbu.text.intValue,sec.text.intValue,swim.text.intValue]; 
     NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:insrt]]; 
     NSString *r=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; 
     NSLog(@"%@",r); 
    } 

服務器端:

<?php 

     $dbhost = 'localhost'; 
     $dbuser = 'root'; 
     $dbpass = ''; 
     $db = 'BuyAndSell'; 
     //$dbtable ='bas'; 
     // Connect Database 
     $conn = mysql_connect($dbhost,$dbuser,$dbpass) or die (mysql_error()); 
     mysql_select_db($db, $conn) or die(mysql_error()); 
     if (isset ($_GET['Zipcode']) && isset ($_GET['Locality']) && isset ($_GET['Propertytype']) && isset ($_GET['pricerange']) && isset ($_GET['Bedrooms']) && isset ($_GET['Bathrooms']) && isset ($_GET['Possession']) && isset ($_GET['Propertyage']) && isset ($_GET['Rating']) && isset ($_GET['Parking']) && isset ($_GET['Powerbackup']) && isset ($_GET['Security']) && isset ($_GET['Swimmingpool'])) 
     { 
      $zipcode=$_GET['Zipcode']; 
      $locality=$_GET['Locality']; 
      $propertytype=$_GET['Propertytype']; 
      $pricerange=$_GET['pricerange']; 
      $bedrooms=$_GET['Bedrooms']; 
      $bathrooms=$_GET['Bathrooms']; 
      $possession=$_GET['Possession']; 
      $propertyage=$_GET['Propertyage']; 
      $rating=$_GET['Rating']; 
      $parking=$_GET['Parking']; 
      $powerbackup=$_GET['Powerbackup']; 
      $security=$_GET['Security']; 
      $swimmingpool=$_GET['Swimmingpool']; 
     } 

     else 
     { 
     echo "failed"; 
     } 

      $sql ="INSERT INTO bas(id, zipcode, locality, propertytype,Pricerange,bedrooms,bathrooms,possession,propertyage,rating,parking,powerbackup,security,swimmingpool)VALUES(NULL,'$zipcode','$locality','$propertytype','$pricerange','$bedrooms', '$bathrooms', '$possession', '$propertyage','$rating','$parking','$powerbackup','$security','$swimmingpool');"; 

      $res = mysql_query($sql,$conn) or die(mysql_error()); 
      if ($res) 
      { 
       echo "Success"; 
      } 
      else 
      { 
       echo "faild to insert"; 
      } 

     /* } 
     else 
     { 
      echo "successrr"; 

    }*/ 
    ?> 

這是印刷failedSuccess這是正確的。問題出在我的客戶端代碼中。它不會將數據存儲在遠程數據庫上。

回答

0

請通過下面的一行代碼改變您的插入字符串可能,這將幫助:

NSString *insrt=[NSString stringWithFormat:@"http://localhost/basInsertv1.php?Zipcode=%i&Locality=%@&Propertytype=%@&pricerange=%@&Bedrooms=%i&Bathrooms=%i&Possession=%@&Propertyage=%@&Rating=%@&Parking=%i&Powerbackup=%i&Security=%i&Swimmingpool=%i",zipcodetf.text.intValue,localitytf.text,proprtytypetf.text,sliderresltlabel.text,bedroomstf.text.intValue,bathrooms.text.intValue,possesiontf.text,propertyAge.text,rating.text,parking.text.intValue,pbu.text.intValue,sec.text.intValue,swim.text.intValue]; 

    NSString *newInsrStr = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
+0

好極了!這是工作。但是,你能解釋爲什麼我的代碼不工作嗎? @Vijay Masiwal –

0

我認爲,這是因爲你在你的URL中使用「本地主機」。 嘗試爲您的服務器輸入正確的IP地址(如192.168.0.1) 提示:127.0.0.1不正確。

您也可以將服務器上的所有訪問記錄到文件中,或者在實際調用該站點時檢入服務器日誌。

你的服務器代碼是可怕的 我還是不喜歡它,但嘗試這樣的:

<?php 
    $dbhost = 'localhost'; 
    $dbuser = 'root'; 
    $dbpass = ''; 
    $db = 'BuyAndSell'; 
    //$dbtable ='bas'; 
    // Connect Database 
    $conn = mysql_connect($dbhost,$dbuser,$dbpass) or die (mysql_error()); 
    mysql_select_db($db, $conn) or die(mysql_error()); 
    if (isset ($_GET['Zipcode']) && isset ($_GET['Locality']) && isset ($_GET['Propertytype']) && isset ($_GET['pricerange']) && isset ($_GET['Bedrooms']) && isset ($_GET['Bathrooms']) && isset ($_GET['Possession']) && isset ($_GET['Propertyage']) && isset ($_GET['Rating']) && isset ($_GET['Parking']) && isset ($_GET['Powerbackup']) && isset ($_GET['Security']) && isset ($_GET['Swimmingpool'])) 
    { 
//remember to escape received data! 
     $zipcode=$_GET['Zipcode']; 
     $locality=$_GET['Locality']; 
     $propertytype=$_GET['Propertytype']; 
     $pricerange=$_GET['pricerange']; 
     $bedrooms=$_GET['Bedrooms']; 
     $bathrooms=$_GET['Bathrooms']; 
     $possession=$_GET['Possession']; 
     $propertyage=$_GET['Propertyage']; 
     $rating=$_GET['Rating']; 
     $parking=$_GET['Parking']; 
     $powerbackup=$_GET['Powerbackup']; 
     $security=$_GET['Security']; 
     $swimmingpool=$_GET['Swimmingpool']; 

     $sql ="INSERT INTO bas(id, zipcode, locality, propertytype,Pricerange,bedrooms,bathrooms,possession,propertyage,rating,parking,powerbackup,security,swimmingpool)VALUES(NULL,'$zipcode','$locality','$propertytype','$pricerange','$bedrooms', '$bathrooms', '$possession', '$propertyage','$rating','$parking','$powerbackup','$security','$swimmingpool');"; 

     // insert into database only if all parameters are given 
     $res = mysql_query($sql,$conn) or die(mysql_error()); 
     if ($res) { 
      echo "Success"; 
     } 
     else { 
      echo "faild to insert"; 
     } 
    } 
    else { 
    echo "failed - data missing"; 
    } 

我也建議使用POST發送數據 - 服務器對GET數據長度的限制

相關問題