2013-02-04 186 views
0

我想插入一些值到MySQL數據庫通過iPad應用程序在線訪問它。插入數據到在線數據庫

將數據輸出:

if(txtInput != nil){ 
     NSString *postVars = [NSString stringWithFormat:@"id=%d&input=%@", index, txtInput.text]; 
     NSLog(@"%d",index); 
     index++; 
     NSData *data = [postVars dataUsingEncoding:NSUTF8StringEncoding]; 

     NSString *strURL = @"http://localhost:8888/Insert.php"; 
     NSURL *url = [NSURL URLWithString:strURL]; 
     NSMutableURLRequest *req = [[NSMutableURLRequest alloc]initWithURL:url]; 
     [req setHTTPBody:data]; 
     [req setHTTPMethod:@"POST"]; 
     NSURLConnection *con = [NSURLConnection connectionWithRequest:req delegate:self]; 
    } 

用於Web服務的腳本:

<?php 
$host = 'localhost'; 
$username = 'root'; 
$password = 'root'; 

$con = mysql_connect($host, $username, $password); 
if(!$con) 
{ 
echo "Failed connection"; 
echo "<br/>"; 
die('Connection failed'); 
} 

mysql_select_db("unbounded", $con); 

$ids = $_GET['id']; 
$str = $_GET['input']; 

$in= mysql_query("INSERT INTO `unbounded`.`stuff` (`id`, `string`) VALUES ('$ids','$str');"); 
echo $in; 
echo "<br/>"; 
mysql_close($con); 
?> 

有一次,我在PHPAdmin頁面運行我的應用程序,我可以看到值ID已被設置爲每次0 I在字符串值爲空時執行應用程序。

如果我通過瀏覽器運行腳本插入數據罰款。順便說一下,一旦我執行腳本vi瀏覽器,我發現與DB的連接已經建立好了。 。 可能是什麼概念? 最佳regrads

回答

1

在iOS應用使用的是POST方法發送數據

[req setHTTPMethod:@"POST"]; 

但在PHP你是從閱讀GET

$ids = $_GET['id']; 

它應該工作,如果同時使用相同的HTTP方法

與您的問題無關,但您的PHP代碼易受攻擊SQL注入攻擊。

+0

而你是非常正確的。感謝那! – uml