簡體   English   中英

從iOS POST到MySQL數據庫

[英]POST from iOS to MySQL database

我正在嘗試將數據從我的UITextFields發布到MySQL數據庫。 控制台說我的連接成功,但是由於某種原因數據沒有發布到數據庫? 知道為什么嗎? 這是我的代碼:

ViewController.h

-(IBAction)addParty:(id)sender;

@property (strong, nonatomic) IBOutlet UITextField *firstname;
@property (strong, nonatomic) IBOutlet UITextField *lastname;
@property (strong, nonatomic) IBOutlet UITextField *phone;
@property (strong, nonatomic) IBOutlet UITextField *email;
@property (strong, nonatomic) IBOutlet UITextField *guests;

@property (nonatomic, copy) NSDictionary *venueDetail;
@end

ViewController.m

- (IBAction)addParty:(id)sender
{

NSString *strURL = [NSString stringWithFormat:@"guestfirst=%@&guestlast=%@", firstname.text, lastname.text];

NSData *postData = [strURL dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://myurl.com/phpfile.php"]];
[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");
}

}

post.php中

<?Php
include('connect.php');

if (isset ($_POST["guestfirst"]) && isset ($_POST["guestlast"])){
    $guestfirst = $_POST["guestfirst"];
    $guestlast = $_POST["guestlast"];
} else {
    $guestfirst = "none";
    $guestlast = "none";
}

// Insert value into DB
$sql = "INSERT INTO events (guestfirst, guestlast) VALUES ('$guestfirst', '$guestlast');";
$res = mysql_query($sql,$conn) or die(mysql_error());

mysql_close($conn);

if($res) {          
$response = array('status' => '1');                 
} else {
die("Query failed");
}

echo json_encode($res);
exit();

?>

我目前的程式碼有一些問題。

  1. 在獲取現有文章時,您將查詢分配給$get_events ,但從$events獲取。
  2. 您在插入之前正在獲取。 (也許這是您的意圖?)
  3. 您輸出多個JSON對象
  4. 您的查詢使用$events而不是`events`
  5. 如果發生MySQL錯誤,則可以調用die() ,因此您將永遠不會收到表示錯誤的JSON響應。
  6. 您正在使用舊的MySQL庫,並且腳本容易受到SQL注入的攻擊。

下面是一個應修復所有舊版本庫的版本:

connect.php

<?php
$conn = mysql_connect('localhost', 'root', 'root');
mysql_select_db('testing');
?>

post.php中

<?php
require_once("connect.php");

if (isset ($_POST["guestfirst"]) && isset ($_POST["guestlast"]))
{
    $guestfirst = mysql_real_escape_string($_POST["guestfirst"]);
    $guestlast = mysql_real_escape_string($_POST["guestlast"]);
}
else
{
    $guestfirst = "No Entry";
    $guestlast = "none";
}

$sql = "INSERT INTO `events` (guestfirst, guestlast) VALUES ('$guestfirst', '$guestlast');";
$res = mysql_query($sql, $conn) or $error = mysql_error();

if($res)
{
    $ret = array(
        'inserted' => true
    );
}
else
{
    $ret = array(
        'inserted' => false,
        'error' => $error
    );
}

mysql_close($conn);
echo json_encode($ret);
exit();
?>

list.php(或您所說的其他名稱)

<?php
require_once("connect.php");

$get_events = mysql_query("SELECT * FROM `events`", $conn);
$articles = array();

while ($row = mysql_fetch_assoc($get_events))
{
    $articles[] = $row;
}

mysql_close($conn);
echo json_encode($articles);
exit();
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM