[英]JAVA / PHP: How to insert multiple rows with java and php into DB? (using mysqli)
I want to insert N number of rows in a mysql table called USERS. 我想在名为USERS的mysql表中插入N行。 For inserting a single row I write this code (it works): 为了插入单行,我编写了以下代码(有效):
PHP side: insert_user.php PHP方面:insert_user.php
<?php
$mysqli = new mysqli('hostname', 'username','password', 'dbname');
if (mysqli_connect_errno()) {
printf("Can't connect Errorcode: %s\n", mysqli_connect_error());
exit;
}
if ($result = $mysqli->prepare("INSERT INTO `users` (`email`, `imei`) VALUES (?,?)")) {
$email = $_POST['email'];
$imei = $_POST['imei'];
$result->bind_param("ss", $email, $imei);
$result->execute();
$result->close();
printf("Insert OK");
} else {
printf("Insert KO. ERROR: %s",mysqli_error());
}
$mysqli->close();
?>
JAVA side JAVA方面
public void insertUser(String email
, String imei) {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("imei", imei));
// send them on their way
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("myserver.com/insert_user.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
} catch (e) {
e.printStackTrace();
}
}
THE QUESTION 问题
I need a little complete example for " insert_ multi _user.php " and the relative java method, to pass more rows (using an Array? a Collection? I don't know...). 我需要一个用于“ insert_ multi _user.php ”和相关java方法的完整示例,以传递更多行(使用Array?Collection?我不知道...)。 For instance, replacing this java lines: 例如,替换以下Java行:
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("imei", imei));
with arrays (?): 与数组(?):
nameValuePairs.add(new BasicNameValuePair("email", email[]));
nameValuePairs.add(new BasicNameValuePair("imei", imei[]));
and, in php, replacing: 并且,在php中,替换为:
$email = $_POST['email'];
$imei = $_POST['imei'];
with: 与:
$email[] = $_POST['email'];
$imei[] = $_POST['imei'];
(obviously the above code is only to give you the idea, it is not correct...) (显然上面的代码只是给你的想法,这是不正确的...)
I spent days with no luck :( Thank you, Geltry 我花了好几天没有运气:(谢谢,格特里
Just wrap your code with for loop 只需用for循环包装代码
If you have a record/array with users you can do something like 如果您与用户有记录/阵列,则可以执行以下操作
$users = array(...);
foreach($users as $user) {
$mysqli->prepare("INSERT INTO `users` (`email`, `imei`) VALUES (?,?)")l
$result->bind_param("ss", $user['email'], $user['imei']);
$result->execute();
$result->close();
}
But to do that you have to change the way you submit your data, so don't do it one by one rather submit a CSV file for example, parse it with fgetcsv
and run through each of the records. 但是要做到这一点,您必须更改提交数据的方式,所以不要一一提交,而要提交CSV文件,例如,使用fgetcsv
解析它并遍历每条记录。
On the Java side you can also pass a POST array: 在Java方面,您还可以传递POST数组:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("user[0][email]","email1"));
nameValuePairs.add(new BasicNameValuePair("user[0][imei]","imei1"));
nameValuePairs.add(new BasicNameValuePair("user[1][email]","email2"));
nameValuePairs.add(new BasicNameValuePair("user[1][imei]","imei2"));
The other options is to send the data in JSON format to the PHP script and use json_decode
其他选项是将JSON格式的数据发送到PHP脚本并使用json_decode
I don't believe you can pass an Array (from Java) into the HTTP post as an argument to the PHP script. 我不相信您可以将数组(来自Java)传递到HTTP帖子中作为PHP脚本的参数。 So, basically, your Java code needs to send 1 user at a time in the POST request to add users... so you wrap that in a loop. 因此,基本上,您的Java代码需要在POST请求中一次发送1个用户以添加用户...因此您将其包装在一个循环中。
There might be a way to serialize the Object Array and send it as a payload to the PHP script where the PHP script can deserialize it, but I am not aware of how you would do such a thing except that you could serialize your array into a XML file. 可能有一种方法可以序列化对象数组,并将其作为有效载荷发送到PHP脚本,PHP脚本可以在该脚本中反序列化它,但是我不知道您将如何执行此操作,只是可以将数组序列化为对象。 XML文件。 The PHP script could then read the XML and loop within the PHP script to add the users. 然后,PHP脚本可以读取XML,并在PHP脚本中循环以添加用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.