douguluan5102 2015-06-04 19:33
浏览 68

从iOS应用程序中的QR扫描(文本)发送数据以存储在SQL数据库中

I have a QR scanner that will read the text that is embedded and I would like it to store the information into the SQL database. The database is setup, and the php site handles the input correctly if I enter manually into the URL (ex. www.blah.com/input.php?name=Bob). However, I cannot figure out how to get the information to submit to my php site and/or put the information into the database. I'm sure there is a better/easier way. Below is the code I am using:

NSString *url= [NSString stringWithFormat:@"http://blah.com/input.php?name=%@", name];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString: url]];

Then this is my current php setup:

<?php 
header('Content-type: text/plain; charset=utf-8');


$db_conn = new  PDO('mysql:host=host;dbname=db','user','pwd');
$db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$date = date("m/d/y");
$time = date("h:i:sa");
//$name = "";
$name = ($_GET['name']); 


$qry = $db_conn->prepare('INSERT INTO CheckIn(`date`,`time`,`name`) VALUES          (:date,:time,:name)');
$qry->bindParam(':date', $date);
$qry->bindParam(':time', $time);
$qry->bindParam(':name', $name);

$qry->execute();

if ($qry) { $message = "success"; }
else { $message = "failed"; }

echo utf8_encode($message);
?>
  • 写回答

1条回答

  • dsbc80836 2015-06-05 23:51
    关注

    You can use NSURLConnection class to send synchronous / asynchronous request.

    Here is an example for the synchronous request:

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"GET"];
    NSURLResponse *response;
    NSError *err;
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request
                                               returningResponse:&response 
                                                           error:&err];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'