在Titanium和PHP之间传递数组的正确方法是什么?

时间:2013-01-07 23:35:49

标签: php titanium

我有一个名为 mobilemanager 的数据库,其中一个名为cc_devices的表在我的wamp服务器上运行并测试,我想将一个数组传递给json对象并将所有数据存储在该json页面中,但我只得到一个价值。一切都完成后,这是我的数据库的图片

enter image description here

这是我在钛中用来传递数组的代码

//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');
    };  

这是PHP代码

<?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();
?>

2 个答案:

答案 0 :(得分:1)

我想在完成所有工作后我就知道了这是我的数据库

enter image description here

这是我对Titanium中的.js文件所做的更改

//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');
    };  

这就是我在.php文件中将所有参数传递给数据库的方法

<?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();
?>

答案 1 :(得分:0)

imm我理解,你在PHP中接收一个json字符串作为参数,我不确定是POST还是GET。

为了帮助测试它,首先在PHP环境中安装krumo。使用krumo($ _ POST)和krumo($ _ GET)来查看你收到的确切内容。向我们提供结果。

我相信PHP知道它正在接收一个字符串,但我不知道用什么密钥来存储该字符串,可能是$ _POST [0] ......你也必须验证它。

一旦变量上有json字符串,就像boruch所说,你必须json_decode()它,它将返回一个数组:

$newArray = json_decode($_POST[0]);
$user = $newArray['user'];
.
.
.