Ajax - >通过php将JS数组存储在mySQl表中

时间:2016-09-21 10:57:46

标签: php jquery mysql ajax

enter image description here我目前正在尝试通过AJAX将数组传递给php,然后将其存储在表中。

php文件 (在wamp服务器上运行)

    <?php

$servername = "localhost";
$username = "root";
$password = "xx";
$dbname = "xx";

$myArray = $_POST['myArr'];

echo $myArray;


$con = mysqli_connect($servername,$username,$password ,$dbname) or die ("could not connect database");

$query = "INSERT INTO inventory_db (timestamp, deviceid, product_name, tray, latitude, longitude, product) VALUES" . $myArray;
$result = mysqli_query($con, $query) or die(mysqli_error($con));

$con->close();

?>

Ajax

    function jsonconversion(){

        jsonString = JSON.stringify(myArr);

        $.ajax({        
       type: "POST",
       url: "http://xx.xx.xx.xx:80/xx/sendData.php",
       data: {myData:myArr},
       complete: function() {
            console.log("Success");
       },
       error: function() {
           console.log("Function: forward_to_server() error")
       }
    }); 
}

最初我尝试使用JSON但我现在还没有看到它的需要......(我应该吗?)

2 个答案:

答案 0 :(得分:0)

几个错字错误:

$ myArray = $ _POST ['myData'];

$ fulldata = json_decode($ myArray);

的print_r($ fulldata); //从完整数据中,您需要获得下面提到的这些变量。

$ query =“INSERT INTO inventory_db(timestamp,deviceid,product_name,tray,latitude,longitude,product)VALUES(NOW(),'$ deviceid','$ product_name','$ myArray','$ try' ,'$ lat','$ long','$ prod');

答案 1 :(得分:0)

所以无论如何我都明白了。供将来使用的phonegap和wamp服务器用户参考:

    $con = mysqli_connect($servername,$username,$password ,$dbname) or die ("could not connect database");

//jsArray is waiting for the Ajax in the android app JS file.
//myArray is placing the JSON object into an array for php to store its values
$jsArray = $_POST['jsonString'];
$myArray = json_decode($jsArray,true);

//the next line is to test and see what happens with an array
//$myArray = array('timestamp' => 1, 'deviceid' => "x1x1x1x1x1x1x1x1x1x1", 'product_name' => "cola", 'tray' => "large", 'latitude' => 5, 'longitude' => 6, 'product_weight'=>7);

//declare variables and use 'key' value in array to input values.
$timestamp = $myArray['timestamp'];
$deviceid = $myArray['deviceid'];
$product_name = $myArray['product1'];
$tray = $myArray['tray'];
$latitude = $myArray['latitude'];
$longitude = $myArray['longitude'];
$product_weight = $myArray['product2'];

//insert into table inventory_db 
$query = "INSERT INTO inventory_db (timestamp, deviceid, product1, tray, latitude, longitude, product2) 
VALUES('$timestamp','$deviceid','$product1','$tray','$latitude','$longitude','$product2')";

$result = mysqli_query($con, $query) or die(mysqli_error($con));

$con->close();

?>

在JS文件中,我将其更改为Object

而不是数组
var myArr = {timestamp:0,deviceid:0,product1:"",type:"",latitude:0,longitude:0, product2:0};

然后

var jsonString = "";
function jsonconversion(){

        jsonString = JSON.stringify(myArr);
        //console.log(jsonString);

        $.ajax({        
       type: "POST",
       url: "http://x.x.x.x:80/x/sendData.php",
       data: {data:jsonString},
       complete: function() {
            console.log("Success");
       },
       error: function() {
           console.log("Function: forward_to_server() error")
       }
    }); 
}

对于briggs回答删除IP,如果我删除Ip并将其保留在文件结构中,它仍然不起作用(虽然如果我只使用WAMP和JS / PHP / AJAX,这确实有用) / p>