我在从iOS应用程序更新数据库时遇到问题...
Sql语句
<?php
// Create connection
$con=mysqli_connect("server_name","user_name","user_code","table_name");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table 'table.name'
$sql = "SELECT * table_name";
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row = $result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
$sql = "INSERT INTO table_name (column_name) VALUES('1')";
// Close connections
mysqli_close($result);
mysqli_close($con);
?>
我的iOS代码
for (Tickets *items in _feedItems){
if ([resultText.text isEqual:(id) items.ticketNumber]) {
status.text = @"Match found";
//Contacting database------------------------------
NSString *strURL = [NSString stringWithFormat:@"TicketDate=%@", items.ticketDate];
NSData *postData = [strURL dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://webservice.com"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-
Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(conn)
{
NSLog(@"Connection Successful");
}
else
{
NSLog(@"Connection could not be made");
}
//Contacting database END-------------------------------------------
break;
}else {
status.text = @"Match not found";
}
}
我从NSLog获得了一个Connection Succesful,但我的数据库中没有任何更新。
我试图用应用程序手动输入票号,如果它与数据库中的票证匹配(它有效),那么我希望ticketDate从0更改为1。
但它没有改变:/
答案 0 :(得分:0)
你没有打电话
[conn start];
有效发送您的请求。即:
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[conn start];
您还应该知道请求将以异步发送,这意味着您在调用start后无法立即获得响应。如果要保持异步连接,应该执行的操作是实现data delegate,至少实现– connection:didReceiveData:
,– connectionDidFinishLoading:
和connection:didFailLoadingWithError:
方法。
或者,您可以考虑同步发送请求,即只替换
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
使用:
[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
但是这种os请求会阻止您的应用UI,除非您在后台线程上发送它们。
答案 1 :(得分:0)
在数据库中,我有一个包含ticketNumber和ticketDate的行。如果我的应用程序中输入的数字与我的数据库中的ticketNumber相同,那么我希望同一行的ticketDate从0更改为1.