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 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序