我想在一个名为 USERS 的 mysql 表中插入 N 行。为了插入单行,我编写了这段代码(它有效):
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端
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();
}
}
问题
我需要一个完整的“ insert_ multi _user.php ”示例和相关的java方法,以传递更多行(使用数组?一个集合?我不知道......)。例如,替换这个 java 行:
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("imei", imei));
使用数组(?):
nameValuePairs.add(new BasicNameValuePair("email", email[]));
nameValuePairs.add(new BasicNameValuePair("imei", imei[]));
并且,在 php 中,替换:
$email = $_POST['email'];
$imei = $_POST['imei'];
和:
$email[] = $_POST['email'];
$imei[] = $_POST['imei'];
(显然上面的代码只是给你一个想法,它是不正确的......)
我度过了没有运气的日子:(谢谢你,Geltry