teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

4

書式の変更

2017/12/22 08:34

投稿

horiguchi034
horiguchi034

スコア6

title CHANGED
File without changes
body CHANGED
@@ -30,6 +30,7 @@
30
30
  > $mysqli = new mysqli('IP', 'user', 'password', 'database');
31
31
  mysql_select_db("test", $sql);
32
32
  mysql_query('SET NAMES utf8', $sql ); # この行を追加する
33
+ $str = @$_POST['str'];
33
34
  $query = "insert into test(id, title) values(8, $str)";
34
35
  $result = mysql_query($query);
35
36
  if(!$result){

3

書式の改善

2017/12/22 08:34

投稿

horiguchi034
horiguchi034

スコア6

title CHANGED
File without changes
body CHANGED
@@ -1,8 +1,8 @@
1
1
  ###前提・実現したいこと
2
- Objective-cから渡した文字列をPHPで受け取りそれをweb viewで表示させるプログラムを作っています
2
+ Objective-cから渡した文字列をPHPで受け取りMysqlのテーブルに追加しそれをweb viewで表示させるプログラムを作っています
3
3
 
4
4
  ###発生している問題・エラーメッセージ
5
- 特にエラーは出ないのですが通信された文字列を受け取り表示することができていません
5
+ 特にエラーは出ないのですが通信された文字列を受け取りテーブルに追加することができていません
6
6
 
7
7
  post先への通信は実行できています
8
8
 
@@ -10,8 +10,9 @@
10
10
 
11
11
 
12
12
  ###---objective-cのソースコード---
13
- > NSURL* url = [NSURL URLWithString:@"URL"];//POST先url
13
+ > NSURL* url = [NSURL URLWithString:@"URL"];//データベース追加用php
14
+ NSURL* url2 = [NSURL URLWithString:@"URL2"]; //データベース表示用php
14
- NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration];
15
+ NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration];
15
16
  NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
16
17
  NSURLSession* session = [NSURLSession sessionWithConfiguration:config];
17
18
  // postするテキスト
@@ -25,13 +26,38 @@
25
26
  NSURLRequest *myRequest = [NSURLRequest requestWithURL:url];
26
27
  [self.webtest loadRequest:myRequest];
27
28
 
29
+ ###---PHPのソースコード(データベースに追加用)---
30
+ > $mysqli = new mysqli('IP', 'user', 'password', 'database');
31
+ mysql_select_db("test", $sql);
32
+ mysql_query('SET NAMES utf8', $sql ); # この行を追加する
33
+ $query = "insert into test(id, title) values(8, $str)";
34
+ $result = mysql_query($query);
35
+ if(!$result){
36
+ die('die'.mysql_error());
37
+ }
38
+
39
+
28
- ###---PHPのソースコード---
40
+ ###---PHPのソースコード(表示用)---
29
41
  > <?php
42
+ $mysqli = new mysqli('IP', 'user', 'password', 'database');
43
+ if ($mysqli->connect_error) {
44
+ echo $mysqli->connect_error;
45
+ exit();
46
+ } else {
47
+ $mysqli->set_charset("utf8");
48
+ }
30
- $a = "posttest";
49
+ $sql = "select * from test";
50
+ if ($result = $mysqli->query($sql)) {
51
+ // 連想配列を取得
52
+ while ($row = $result->fetch_assoc()) {
31
- print "$a</br>";
53
+ echo $row["id"] . $row["title"] . "<br>";
32
- $str = @$_POST['str'];
54
+ }
33
- print "$str";
55
+ }else{
34
- print("end");
56
+ print("incorrect");
57
+ }
58
+ $result -> close();
59
+ // DB接続を閉じる
60
+ $mysqli->close();
35
61
  ?>
36
62
 
37
63
  ###試したこと

2

書式の改善

2017/12/22 08:29

投稿

horiguchi034
horiguchi034

スコア6

title CHANGED
File without changes
body CHANGED
@@ -29,10 +29,8 @@
29
29
  > <?php
30
30
  $a = "posttest";
31
31
  print "$a</br>";
32
-
33
32
  $str = @$_POST['str'];
34
33
  print "$str";
35
-
36
34
  print("end");
37
35
  ?>
38
36
 

1

書式の改善

2017/12/22 08:07

投稿

horiguchi034
horiguchi034

スコア6

title CHANGED
File without changes
body CHANGED
@@ -9,29 +9,24 @@
9
9
 
10
10
 
11
11
 
12
- ###---objective-cのソースコード---**
12
+ ###---objective-cのソースコード---
13
- NSURL* url = [NSURL URLWithString:@"URL"];//POST先url
13
+ > NSURL* url = [NSURL URLWithString:@"URL"];//POST先url
14
14
  NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration];
15
15
  NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
16
- NSURLSession* session = [NSURLSession sessionWithConfiguration:config];
16
+ NSURLSession* session = [NSURLSession sessionWithConfiguration:config];
17
-
18
17
  // postするテキスト
19
- NSData* data = [@"str=vvvv" dataUsingEncoding:NSUTF8StringEncoding];
18
+ NSData* data = [@"str=vvvv" dataUsingEncoding:NSUTF8StringEncoding];
20
-
21
19
  request.HTTPMethod = @"POST";
22
20
  request.HTTPBody = data;
23
21
  NSURLSessionDataTask* task = [session dataTaskWithRequest:request
24
- completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
22
+ completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { // 完了時の処理
25
- // 完了時の処理
26
23
  }];
27
-
28
- [task resume];
24
+ [task resume];
29
-
30
25
  NSURLRequest *myRequest = [NSURLRequest requestWithURL:url];
31
26
  [self.webtest loadRequest:myRequest];
32
27
 
33
28
  ###---PHPのソースコード---
34
- <?php
29
+ > <?php
35
30
  $a = "posttest";
36
31
  print "$a</br>";
37
32