gpt4 book ai didi

php - Android 使用 PHP 添加多个 MYSQL 行

转载 作者:行者123 更新时间:2023-11-29 13:07:52 25 4
gpt4 key购买 nike

基本上我知道如何使用 android 添加一行到外部 mysql 数据库。现在我希望一次添加多行。到目前为止,这是我的代码。

PHP 脚本

<?php
$host="db4free.net"; //replace with database hostname
$username="xxxxxx"; //replace with database username
$password="xxxxxxx"; //replace with database password
$db_name="xxxxxxx"; //replace with database name

$con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
/* Get the value in the whichfunction parameter
sent from the android application which will
determine the fucntion to call. */

$getFunctionToCall = $_POST['whichfunction'];


/* Depending on the value of the whichfunction
parameter switch to call different function */

switch ($getFunctionToCall){
case "AddUser":
echo AddUser($_POST['name'],$_POST['password']);
break;
}

/* Function to add user to the user table */

function AddUser($name,$password){
$sql = "insert into users(name,password) values('$name','$password')";
if(mysql_query($sql)){
return 1; // Return 1 for success;
}else{
return 2;// Return 2 for database error;
}
}
?>

安卓方法

protected void SendToPhpFile() {

ArrayList<NameValuePair> pp = new ArrayList<NameValuePair>();


pp.add(new BasicNameValuePair("whichfunction", "AddUser"));

String[] names = {"David","Jimmy","Shane"};
String[] passwords = {"mypass1","mypass2","mypass3"};



for (int i=0;i<names.length;i++){
pp.add(new BasicNameValuePair("names[]", String.valueOf(names[i])));
pp.add(new BasicNameValuePair("passwords[]", String.valueOf(passwords[i])));
}


try{
status = "";
status = CustomHttpClient.executeHttpPost(ConnectBase.link, pp);
String res=status.toString();
res= res.replaceAll("\\s+","");

/* Depending on value you return if insert was successful */
if(res.equals("1")){
Toaster("Data successfully added.");
}else{
Toaster(status);
}
}catch(Exception e){
Toaster("Data successfully added: " + e.toString());
}

}

所以我认为我已经或多或少地对 android 尺寸进行了排序,但我不是 100%。如果你们能针对我的开始方式提出一个解决方案,那就太好了。我对 php 的经验为零,所以在添加超过 1 行时不知道从哪里开始。谢谢

最佳答案

也许是这样的?

INSERT INTO table (name, password ) VALUES ('Name1', 'Password1'), ('Name2', 'Password2'),('Name3', 'Password3'), ('Name4', 'Password4')

关于php - Android 使用 PHP 添加多个 MYSQL 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22466714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com