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

我有一个QR扫描器,它将读取嵌入的文本,我希望它将信息存储到 SQL数据库。 数据库已设置,如果我手动输入URL(例如 www.blah.com/input.php?name=Bob </ code>),php站点将正确处理输入。 但是,我无法弄清楚如何将信息提交到我的php站点和/或将信息放入数据库。 我相信有更好/更简单的方法。 下面是我正在使用的代码:</ p>

  NSString * url = [NSString stringWithFormat:@“http://blah.com/input.php?name=%@”,  name]; 
NSData * dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
</ code> </ pre>

然后这是我当前的php设置:</ p> \ n

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

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

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

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

$ qry-&gt; execute();

if($ qry){$ message =“success”; }
else {$ message =“失败”; }

echo utf8_encode($ message);
?&gt;
</ code> </ pre>
</ div>

展开原文

原文

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个回答



您可以使用NSURLConnection类发送同步/异步请求。 </ p>

以下是同步请求的示例:</ p>

  NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url  ]]; 
[请求setHTTPMethod:@“GET”];
NSURLResponse * response;
NSError * err;
NSData * returnData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&amp; response
error:&amp; err] ;
</ code> </ pre>
</ div>

展开原文

原文

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];

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐