Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Android插入多个条目->;PHP->;MYSQL_Php_Android_Mysql_Mysqli_Bulkinsert - Fatal编程技术网

从Android插入多个条目->;PHP->;MYSQL

从Android插入多个条目->;PHP->;MYSQL,php,android,mysql,mysqli,bulkinsert,Php,Android,Mysql,Mysqli,Bulkinsert,我正在尝试将多个(1-50)条目从Android应用程序插入外部Mysql数据库。我完全有一个PHP脚本来处理单插入查询。但到目前为止,我未能使整个条目数组都能正常工作,很可能是因为我对PHP的理解有限 Android代码: List<NameValuePair> upload_array = new ArrayList<NameValuePair>(); upload_array.add(new BasicNameValuePair("mFirstname[0]", "

我正在尝试将多个(1-50)条目从Android应用程序插入外部Mysql数据库。我完全有一个PHP脚本来处理单插入查询。但到目前为止,我未能使整个条目数组都能正常工作,很可能是因为我对PHP的理解有限

Android代码:

List<NameValuePair> upload_array = new ArrayList<NameValuePair>();
upload_array.add(new BasicNameValuePair("mFirstname[0]", "FirstName 1"));
upload_array.add(new BasicNameValuePair("mFirstname[1]", "FirstName 2"));
upload_array.add(new BasicNameValuePair("mLastname[0]", "LastName 1"));
upload_array.add(new BasicNameValuePair("mLastname[1]", "LastName 2"));
upload_array.add(new BasicNameValuePair("mNickname[0]", "NickName 1"));
upload_array.add(new BasicNameValuePair("mNickname[1]", "NickName 2"));

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://url/script.php");
HttpResponse response = null;
try {
    httppost.setEntity(new UrlEncodedFormEntity(upload_array));
    response = httpclient.execute(httppost);
} catch (Exception e) {
    e.printStackTrace();
}
List upload_array=new ArrayList();
upload_array.add(新的BasicNameValuePair(“mFirstname[0]”,FirstName 1”);
upload_array.add(新的BasicNameValuePair(“mFirstname[1],“FirstName 2”);
upload_array.add(新的BasicNameValuePair(“mLastname[0]”,LastName 1”);
upload_array.add(新的BasicNameValuePair(“mLastname[1]”,LastName 2”);
upload_array.add(新的BasicNameValuePair(“mNickname[0]”,昵称1”);
upload_array.add(新的BasicNameValuePair(“mNickname[1],“昵称2”);
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://url/script.php");
HttpResponse响应=null;
试一试{
setEntity(新的UrlEncodedFormEntity(上传数组));
response=httpclient.execute(httppost);
}捕获(例外e){
e、 printStackTrace();
}
在PHP中:

<?php

$connect = mysqli_connect("***","***","***", "***");

if(mysqli_connect_errno($connect))
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
    echo "success";
}

$query = mysqli_prepare("INSERT INTO `namelist` (`firstname`,`lastname`,`nickname`)
    VALUES(?,?,?)");

$mFirstname = $_POST['mFirstname'];
$mLastname = $_POST['mLastname'];
$mNickname = $_POST['mNickname'];

foreach($mFirstname as $key as $key => $value) {
    $query->bind_param('sss',$value["mFirstname"],$value["mLastname"],$value["mNickname"];
    $query->execute();
}

mysqli_close($connect);
?>

好的,我使用JSON数组完成了这项工作。如果有人使用它,下面是它的用法:

Android,创建JSON字符串:

//Create JSON string start
String json_string ="{\"upload_fishes\":[";

//Repeat and loop this until all objects are added (and add try+catch)
JSONObject obj_new = new JSONObject();
obj_new.put("fish_id", your_looped_string_1[i]);
obj_new.put("fish_lat", your_looped_string_2[i]);
obj_new.put("fish_lon", your_looped_string_3[i]);
json_string = json_string + obj_new.toString() + ",";

//Close JSON string
json_string = json_string.substring(0, json_string.length()-1);
json_string += "]}";
Android将数据发送到PHP(添加try+catch):

PHP脚本:

<?php

//CONNECT TO THE DATABASE
 $DB_HOST = 'yourhost.com';
 $DB_USER = 'user';
 $DB_PASS = 'password';
 $DB_NAME = "db_name";

 $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

 if(mysqli_connect_errno())
{
//    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
//    echo "Connected to MySQL";
}


   $postdata = file_get_contents("php://input"); 
   $data = json_decode($postdata, true);

   if (is_array($data['upload_fishes'])) {
      foreach ($data['upload_fishes'] as $record) {
        $fid = $record['fish_id'];
        $flat = $record['fish_lat'];
    $flon = $record['fish_lon'];

        mysqli_query($mysqli,"INSERT INTO `fishes`(`fish_type_id`, `fish_lat`, `fish_lon`) VALUES ($fid, $flat, $flon)");
      }
   }


mysqli_close($mysqli);
?>

好的,我使用JSON数组完成了这项工作。如果有人使用它,下面是它的用法:

Android,创建JSON字符串:

//Create JSON string start
String json_string ="{\"upload_fishes\":[";

//Repeat and loop this until all objects are added (and add try+catch)
JSONObject obj_new = new JSONObject();
obj_new.put("fish_id", your_looped_string_1[i]);
obj_new.put("fish_lat", your_looped_string_2[i]);
obj_new.put("fish_lon", your_looped_string_3[i]);
json_string = json_string + obj_new.toString() + ",";

//Close JSON string
json_string = json_string.substring(0, json_string.length()-1);
json_string += "]}";
Android将数据发送到PHP(添加try+catch):

PHP脚本:

<?php

//CONNECT TO THE DATABASE
 $DB_HOST = 'yourhost.com';
 $DB_USER = 'user';
 $DB_PASS = 'password';
 $DB_NAME = "db_name";

 $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

 if(mysqli_connect_errno())
{
//    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
//    echo "Connected to MySQL";
}


   $postdata = file_get_contents("php://input"); 
   $data = json_decode($postdata, true);

   if (is_array($data['upload_fishes'])) {
      foreach ($data['upload_fishes'] as $record) {
        $fid = $record['fish_id'];
        $flat = $record['fish_lat'];
    $flon = $record['fish_lon'];

        mysqli_query($mysqli,"INSERT INTO `fishes`(`fish_type_id`, `fish_lat`, `fish_lon`) VALUES ($fid, $flat, $flon)");
      }
   }


mysqli_close($mysqli);
?>


是的,对于复杂数据,使用json和请求正文听起来更好。您好!我对JSON有问题。我试过你的样品,做了一些调整,第一次成功了。但我突然发现了错误,就像格式错误的JSON一样。当我测试和验证JSON代码时,它通过了,但当从应用程序发送到PHP脚本时,我得到了错误。似乎这是一个与编码甚至都设置为UTF8。是的,使用json和请求体听起来更适合复杂的数据。嗨!我对JSON有问题。我试过你的样品,做了一些调整,第一次成功了。但我突然发现了错误,就像格式错误的JSON一样。当我测试和验证JSON代码时,它通过了,但当从应用程序发送到PHP脚本时,我得到了错误。似乎这是编码的东西,甚至都设置为UTF8。