doufen2769 2014-03-17 22:24
浏览 31

Android使用PHP添加多个MYSQL行

Basically I know how to add one row to an external mysql database using android. Now im looking to add multiple rows at once. Heres my code so far.

PHP Script

<?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;
    }
}
?>

Android Method

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());
        } 

}

So I think I have the android size more or less sorted but I'm not 100%. If you guys could come up with a solution to how I've started that would be great. I've got zero experience with php so don't really know where to start when adding more than 1 row. Thank you

  • 写回答

2条回答 默认 最新

  • douzhenzu0247 2014-03-17 22:32
    关注

    Something like this maybe?

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

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?