插入多个MySQL行Android版的PHP [英] inserting multiple mysql rows php Android
问题描述
我试着用PHP脚本和Android外部数据库中插入多个MySQL行。我通过一个JSONObject从Android和我试着去使用这些数据来一次更新MySQL数据库的多行PHP脚本。问题是在PHP脚本
Im trying to insert multiple mysql rows in an external database using a php script and Android. I pass a JSONObject to the PHP script from Android and Im trying to use this data to update multiple rows of a mysql database at once. The problem is in the PHP script
PHP
$con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$json = $_POST['json'];
$array = json_decode($json, true);
for ($i = 0; $i < 3; $i++) {
$name = $array['person'][0]['name'];
$password = $array['person'][0]['password'];
$sql = "insert into Test4Upload(name,password) values('$name','$password')";
if(mysql_query($sql)){
return 1; // Return 1 for success;
}else{
return 2;// Return 2 for database error;
}
}
?>
这是在JSONObject的传递
{人:[{时代:0,名:吉姆},{时代:1,名:哈利},{时代:2,名:法案}]}
This is the JSONObject passed in {"person":[{"age":0,"name":"Jim"},{"age":1,"name":"Harry"},{"age":2,"name":"bill"}]}
这仅更新一次数据库,而不是三个times..Im想知道为什么会这样,而且如果我改变[0]到[I]则数据库根本不更新?
This only updates the database once rather than three times..Im wondering why this is so, and also If I change [0] to [i] then the database does not update at all?
对不起林用于在Java编码,所以我不知道为什么循环迭代的工作不一样,
Sorry Im used to coding in java so I have no idea why the loop iterations dont work the same,
感谢您。
推荐答案
试试这个
$json = $_POST['json'];
$count=0;
$array = json_decode($json, true);
foreach ($array['person'] as $item){
$age= $item['age'];
$name= $item['name'];
$sql = "insert into Test4Upload(age,name) values('$age','$name')";
if(mysql_query($sql))
{
$count++;
}
}
return $count;
这篇关于插入多个MySQL行Android版的PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!