Swift代码无法更新MySQL数据库 [英] Swift code not updating MySQL database

查看:125
本文介绍了Swift代码无法更新MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是Swift 3 iOS代码不会更新mysql数据库,它应该在初始化sendData()代码以添加+1并更新testPop列数据库时进行更新.

My problem is that the Swift 3 iOS code doesn't update the mysql database, it's supposed to whenever the sendData() code is initialized to add +1 and update the testPop column database.

我的应用程序具有一个包含不同对象行的表格视图,我在每行中都有一个按钮,该按钮使indexPath.row知道单击了哪一行中的哪个按钮.使用该信息,可以假定我的sendData()代码将+1添加到特定行的testPop中.

My app has a tableview with rows of different objects, I have a button in each row which gets the indexPath.row to know which button in which row it was clicked in. Using that information, my sendData() code is supposed to add +1 to testPop of the specific row.

希望如此,谢谢.

Swift 3代码:将数据发送到数据库:

func sendData() {

    let postDataURL = "http://exampleip.com/Send.php"
    let url: NSURL = NSURL(string: postDataURL)!
    let request: NSMutableURLRequest = NSMutableURLRequest(url:url as URL)

    let bodyData = String(1)

    request.httpMethod = "POST"
    request.httpBody = bodyData.data(using: String.Encoding.utf8)
    NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue: OperationQueue.main)
    {
        (response, data, error) in
        print(response!)

        if let httpResponse = response as? HTTPURLResponse {
            let statusCode = httpResponse.statusCode

            if statusCode==200 {
                print("Connection Successful")

            } else {
                print("Connection Failed (!200)")
            }
        }
    }
}

PHP代码:

<?php

$host = "host";
$db = "db";
$user = "user";
$pass = "pass";

$connection = mysql_connect($host,$user,$pass);

// Guessing: Posting into MySQL Object
$id = $_POST["id"];

// Checking if connection can be established
if(!$connection){
    die("Connection Failed");
}
else
{
    // Selecting Database
    $dbconnect = mysql_select_db($db, $connection);

    // Check if it can connect to Database
    if(!$dbconnect){
        die("Unable to connect to Database");
    }
    else
    {
        $query = sprintf("UPDATE tests SET testPop=testPop+1 WHERE id = %d", $id);

        $resultset = mysql_query($query, $connection);

        echo "Successfully added";
        echo $query;
    }
}

?>

MySQL代码:

CREATE TABLE IF NOT EXISTS `tests` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`testName` varchar(255) DEFAULT NULL,
`testPop` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;

INSERT INTO `tests` (`id`, `testName`, `testPop`) VALUES
(1, 'Test 1', '0'),
(2, 'Test 2', '0'),
(3, 'Test 3', '0'),
(4, 'Test 4', '0'),
(5, 'Test 5', '0'),
(6, 'Test 6', '0'),
(7, 'Test 7', '0'),
(8, 'Test 8', '0'),
(9, 'Test 9', '0'),
(10, 'Test 10', '0'),
(11, 'Test 11', '0'),
(12, 'Test 12', '0');

推荐答案

由于您在php代码中阅读了id,因此需要发布正文包括id:

since you read the id in your php code you need the post body include the id:

let bodyData = String(1)

应该是这样:

let itemId = String(1)
let bodyData = "id=\(itemId)"

这篇关于Swift代码无法更新MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆