Hey -
I have asked this question before and thought I was going down the right path of figuring it out and turns out that I did not. I am going to provide snippets of code I have tried the results that I have received.
My goal is to send data from my sqlite db to a webserver db
this is how i extract the db from the sqlite db:
var dataArray = [] var rows = db.execute('SELECT * from properties') while (rows.isValidRow()) { dataArray.push({address:'' + rows.fieldByName('address') + '', city:'' + rows.fieldByName('city') + '', state:'' + rows.fieldByName('state') + ''}); rows.next(); };to send the data i use this
var JSONstring = JSON.stringify(dataArray); var loginReq = Titanium.Network.createHTTPClient(); loginReq.open("POST","http://localhost/arrays.php"); loginReq.send( JSONstring );on the server side I do this
$json = $_POST; $jsondecoded = json_decode($json); $username = $json; $tableselectall = "SELECT * FROM users"; $rownum = mysql_num_rows(mysql_query($tableselectall)); $sql2 = "INSERT INTO users (id, username, password, name, email) VALUES('$rownum'+1, '$username', 'test3', 'test3', 'test3@test.com')"; mysql_query($sql2);My results in my phpMySql db for the username is:
'Array'
if i do this in php
$username = $json[0];My results in my phpMySql db for the username is:
' ' (a blank space)
if do this in php
$username = $jsondecoded
My results in my phpMySql db for the username is:
' ' (a blank space)
if do this in php
$username = $jsondecoded[0]no new entry is placed in my db
for reference 'Titanium.API.log(JSONstring)' outputs
[{"address":"Adf","city":"Lk","state":"Lk"},{"address":"Adf","city":"Lk","state":"Lk"},{"address":"Adf","city":"Lk","state":"Lk"},{"address":"Adf","city":"Lk","state":"Lk"},{"address":"Adf","city":"Lk","state":"Lk"},{"address":"Adf","city":"Lk","state":"Lk"},{"address":"Adf","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lka","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"LK"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Fuck","city":"Yeah","state":"Ga"},{"address":"Fuck","city":"Yeah","state":"Ga"},{"address":"Fuck","city":"Yeah","state":"Ga"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"Something","city":"Toronto","state":"Ca"},{"address":"Something","city":"Toronto","state":"Ca"},{"address":"Fuck","city":"Fuck","state":"Fuck"},{"address":"Fuck","city":"Fuck","state":"Fuck"},{"address":"Fuck","city":"Fuck","state":"Fuck"},{"address":"Lk","city":"Lk","state":"Lk"},{"address":"124","city":"Lk","state":"Lk"},{"address":"124","city":"Lk","state":"Lk"},{"address":"3223","city":"Atlanta","state":"Gas"},{"address":"3223","city":"Atlanta","state":"Gas"},{"address":"3223","city":"Atlanta","state":"Gas"},{"address":"5606 ","city":"Stone mountain","state":"Ga"}]My goal is to be able to just pull the first entry for address...
2 Answers
You're sending the data in such a way that its not trying to send a string, its actually trying to set multiple variables. Change your send to:
loginReq.send({ data:JSONstring });Then in your php, read in your post data as:
$json = $_POST['data'];After that, you should be printing out your data to the error log way before you get the database. Just to ensure that you have the right data at each step. So print out json and jsondecoded to the error log so that you can see the data is coming in correctly. After that, you should be able to access your data and insert it into the database.
There is no need to stringify object before sending in line
var JSONstring = JSON.stringify(dataArray);
The HTTPClient's send() method automatically stringifies data for you.So try like this:
var JSONstring = dataArray; and now send it.
Your Answer
Think you can help? Login to answer this question!