0

I have a database called mobilemanager with one table named cc_devices it's up and tested in my wamp server, i want to pass an array to a json object and store all the data in that json page, but i'm only getting one value. this a a picture of my database after everything is done

enter image description here

This is the code that i'm using in titanium to pass an array

//insert into a databse
function submit_Device () {
    //var request = Ti.Network.createHTTPClient();
    var request = Ti.Network.createHTTPClient({
    onload: function(e){
         Ti.API.debug(this.responseXML);
        //alert('The connection was successful!');
    },
    onerror: function(e){
        Ti.API.debug(e.error);
        alert('There was an error during submission.');
    },
    timeout:5000,
    autoEncodeUrl:false,
    }); 
    request.open("POST","http://192.168.34.53/insertuser.php");
    var params = ({"id": "4", "user": "Tony Montana" , "device": "Galaxy s2" , "project": "ABA" , "date": "7/1/2013" , "hour": "4:14 pm" , "used": "True"});  
    request.send(params);
    alert('Device Summited');
    };  

And this is the PHP code

<?php
$username="root";
$password="somepass";
$database="somedb";

mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$user = $_POST['user'];
$query="INSERT INTO somedb.cc_devices (user) VALUES ('" . $user . "')"; 
mysql_query($query);
mysql_close();
?>
Mario Galván
  • 3,964
  • 6
  • 29
  • 39

2 Answers2

1

I think i got it this is my database now after everything is done

enter image description here

This are the changes that i did to my .js file in Titanium

//insert into a databse
function submit_Device () {
    //var request = Ti.Network.createHTTPClient();
    var request = Ti.Network.createHTTPClient({
    onload: function(e){
         Ti.API.debug(this.responseXML);
        //alert('The connection was successful!');
    },
    onerror: function(e){
        Ti.API.debug(e.error);
        alert('There was an error during submission.');
    },
    timeout:5000,
    autoEncodeUrl:false,
    }); 
    request.open("POST","http://192.168.34.53/insertuser.php");
    var params = ({"id": "0" ,"user": "Tony Montana" , "device": "Galaxy s2" , "project": "ABA" , "date": getDate() , "hour": getTime() , "used": "true"});  
    //insert into mobilemanager.cc_devices values (0,"Mario Galván", "IPhone 3g", "ITexico", "2013-08-01", "3:52:52", "true")
    request.send(params);
    alert('Device Summited');
    };  

And this is how i'm passing all the arguments to the database in .php file

<?php
$username="root";
$password="somepass";
$database="somedb";

mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$user = $_POST['user'];
$device = $_POST['device'];
$project = $_POST['project'];
$date = $_POST['date'];
$hour = $_POST['hour'];
$used = $_POST['used'];
$query="INSERT INTO somedb.cc_devices (user,device,project,date,hour,used) VALUES ('$user', '$device', 
    '$project', '$date','$hour', '$used')"; 
mysql_query($query);
mysql_close();
?>
Mario Galván
  • 3,964
  • 6
  • 29
  • 39
0

imm as I understood, you are receiving a json string as parameter in PHP, I'm not sure if as POST or GET.

To help test it, first install krumo on your PHP environment. use krumo($_POST) and krumo($_GET) to see what exactally you're receiving. Provide us the result.

I believe PHP understands it's receiving a string, but I don't know what key is being used to store that string, maybe $_POST[0]... you also must verify that.

Once you have the json string on a variable, as boruch said, you must json_decode() it, which will return an array:

$newArray = json_decode($_POST[0]);
$user = $newArray['user'];
.
.
.
Hikari
  • 3,797
  • 12
  • 47
  • 77