php - RESTful Web Service in iPhone: Data not inserted to database -


i started out doing tutorial using 'shapes' web services comes mamp. trying recreate service myself. have been looking solutions or how debug problem past 5 hours. forte not php, not sure how can setup debug or sort of way print out url response. anyone's appreciated.

objective-c code:

nsstring* servicerooturlstring = @"http://localhost:8888/answers/answers/"; nsurl* answerservice = [nsurl urlwithstring:servicerooturlstring]; _database = [[database alloc] initwithserviceurl:answerservice];  - (void)nextquestionwithanswer:(nsstring *)answer andcomment:(nsstring *)comment {     nsstring* deviceid = [[uidevice currentdevice] uniqueidentifier];     nsdata* deviceidasdata = [deviceid datausingencoding:nsutf8stringencoding];     nsstring* devicehash = [database md5hash:deviceidasdata];      nsmutabledictionary* answerdictionary = [nsmutabledictionary dictionary];     [answerdictionary setobject:devicehash forkey:@"setid"];     [answerdictionary setobject:[nsnumber numberwithint:(_currentquestionnumber + 1)] forkey:@"questionid"];     [answerdictionary setobject:answer forkey:@"answer"];     [answerdictionary setobject:comment forkey:@"comment"];      [_database insertrecordwithdictionary:answerdictionary];      [self nextquestion]; }  - (bool)insertrecordwithdictionary:(nsdictionary *)recorddictionary {     nsdata* recordpropertylistdata = [nspropertylistserialization datafrompropertylist:recorddictionary format:nspropertylistxmlformat_v1_0 errordescription:nil];      nsmutableurlrequest* urlrequest = [nsmutableurlrequest requestwithurl:_serviceurl];     [urlrequest sethttpmethod:@"post"];     [urlrequest sethttpbody:recordpropertylistdata];      nsurlresponse* response = nil;     nserror* error = nil;      nsdata* responsedata = [nsurlconnection sendsynchronousrequest:urlrequest returningresponse:&response error:&error];      id propertylist = [nspropertylistserialization propertylistwithdata:responsedata options:nspropertylistimmutable format:nil error:nil];      nsdictionary* responsedictionary = (nsdictionary*)propertylist;      nslog(@"new record result: %@", responsedictionary);      if (error == nil)     {         return yes;     }     else     {         nslog(@"database error: %@", [error description]);          return no;     } } 

php code:

<?php require_once 'cfpropertylist/cfpropertylist.php';  // connect database $connection = mysql_connect("localhost:8889","root","root"); if (!$connection)     die("could not connect: " . mysql_error());  // database layout $databasename = "answers"; $surveyanswerstablename = "answers"; $surveyanswersarrayname = "answers"; $answerid = "id"; $answertimestamp = "timestamp"; $answersetid = "setid"; $answerquestionid = "questionid"; $answeranswer = "answer"; $answercomment = "comment";  // determine requested resource, stripping empty resource elements , base name $base = "answers"; $resourcekeys = array_merge(array_diff(explode("/", $_server[request_uri]), array("", $base)));  // detect usage of old setup if (count($resourcekeys) == 0)     die("specify table contains shapes. ex: http://the_host_name:8888/answers/your_user_name/");  // use first resource key table name, strip away $surveyanswerstablename = $resourcekeys[0]; $resourcekeys = array_merge(array_diff($resourcekeys, array($surveyanswerstablename)));  // check database. create database , populate if not exist $databaseexists = mysql_select_db($databasename, $connection); if (!$databaseexists) {     // create , select ozzie database     mysql_query("create database $databasename",$connection);     $databaseexists = mysql_select_db($databasename, $connection);     if (!$databaseexists)         die("could not create database $databasename: " . mysql_error()); }  // check requested answers table $sql = "show tables '$surveyanswerstablename'";  mysql_query($sql, $connection); $row = mysql_fetch_array($result); print($row);  // create if not exist if ($row == false) {     // create table holds answers     $sql = "create table $surveyanswerstablename      (         $answerid int not null auto_increment primary key,         $answertimestamp timestamp default current_timestamp on update current_timestamp,         $answersetid text,         $answerquestionid int,          $answeranswer text,         $answercomment text     )";      mysql_query($sql, $connection); }  if ($_server[request_method] == "post") {     // insert or append shape specified in post database     if (count($resourcekeys) == 0)     {         // load posted plist property list object         // hack: save post data file load plist. should able load directly php://input, won't...         $postdata = file_get_contents("php://input");         $filename = dirname(__file__) . "/tmp.php";         $file = fopen($filename, 'w') or die("cannot open file");         fwrite($file, $postdata);         fclose($file);         $postplist = new cfpropertylist($filename);         unlink($filename);          // unpack data answer         // todo: verify data         $answerdictionary = $postplist->getvalue(true);         $setid = $answerdictionary->get($answersetid);         $questionid = $answerdictionary->get($answerquestionid);         $answer = $answerdictionary->get($answeranswer);         $comment = $answerdictionary->get($answercomment);          // insert answer database         $sql = "insert $surveyanswerstablename         (             $answersetid,             $answerquestionid,              $answeranswer,              $answercomment         )          values          (             '$setid',             '$questionid',             '$answer',             '$comment'         )";          mysql_query($sql,$connection);          print($sql);          // package result outer dictionary         // todo: call method instead          $resultdictionary = new cfdictionary();         $resultdictionary->add($surveyanswersarrayname, new cfstring("answer inserted."));          // package outer dictionary property list , transmit         $resultplist = new cfpropertylist();         $resultplist->add($resultdictionary);         header("content-type: text/xml");         print($resultplist->toxml(true));     }     else if (count($resourcekeys) >= 1)     {         // load posted plist property list object         // hack: save post data file load plist. should able load directly php://input, won't...         $postdata = file_get_contents("php://input");         $filename = dirname(__file__) . "/tmp.php";         $file = fopen($filename, 'w') or die("cannot open file");         fwrite($file, $postdata);         fclose($file);         $postplist = new cfpropertylist($filename);         unlink($filename);          // unpack data shape         // todo: verify data         $answerdictionary = $postplist->getvalue(true);         $setid = $answerdictionary->get($answersetid);         $questionid = $answerdictionary->get($answerquestionid);         $answer = $answerdictionary->get($answeranswer);         $comment = $answerdictionary->get($answercomment);          // determine requested shape         $requestedanswersetid = $resourcekeys[0];          // query re-number shapes         $sql = "update $surveyanswerstablename set $answerid = $answerid + 1 $answerid >= $requestedanswerid";         print($sql);         $result = mysql_query($sql);          // insert shape database         $sql = "insert $surveyanswerstablename         (             $answersetid,             $answerquestionid,              $answeranswer,              $answercomment         )          values          (             '$setid',             '$questionid',             '$answer',             '$comment'         )";          mysql_query($sql,$connection);          print($sql);          // package result outer dictionary         // todo: call method instead         // todo: verify add completed         $resultdictionary = new cfdictionary();         $resultdictionary->add($surveyanswersarrayname, new cfstring("answer inserted."));          // package outer dictionary property list , transmit         $resultplist = new cfpropertylist();         $resultplist->add($resultdictionary);         header("content-type: text/xml");         print($resultplist->toxml(true));     }     else          die("invalid request"); }  ?> 

i know asking lot analyze code appreciated.

for print response using ios , xcode:

nsdata* responsedata = [nsurlconnection sendsynchronousrequest:urlrequest returningresponse:&response error:&error]; nslog(@"reponse: %@", [[[nsstring alloc]initwithdata:responsedata encoding:nsutf8stringencoding] autorelease]); 

for check php response without ios create somewhere html file html form contains inputs. inputs must have same keys , values in recorddictionary variable. form action should in servicerooturlstring variable, method post.

see tutorial more information html forms


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -