在我的Android应用中,我创建了一个JSON数组,如:
[{"phone_number":"12345678"},{"phone_number":"23456789"},{"phone_number":"34567890"}, etc... etc...
我的Android代码如下所示:
public static final String KEY_PHONENUMBER = "phonenumber";
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
//The KEY, KEY_PHONENUMBER = "phonenumber" . In PHP we will have $_POST["phonenumber"]
//The VALUE, phonenumber, will be of the form "12345678"
params.put(KEY_PHONENUMBER,jsonArrayContacts.toString());
return params;
}
如何将这些电话号码插入我的用户表?
我知道Php插入代码本身就是:
$insert_into_user_command = "INSERT INTO user WHERE username = '$value'";
$insert_into_contacts_table = mysqli_query($con,$insert_into_contacts_command);
但你能告诉我确切的代码吗?做一个单独的值很容易,但无法弄清楚如何在一个POST中插入它们。
我知道我在正确的轨道上,因为我在Android中的回复显示了我的JSON数组中的所有电话号码:
require('dbConnect.php');
$json = $_POST['phonenumber'];
$array = json_decode($json);
foreach ($array as $value)
{
echo $value->phone_number ;
}
答案 0 :(得分:0)
<?php
require('dbConnect.php');
$json = $_POST['phonenumber'];
$array = json_decode($json);
foreach ($array as $value)
{
$phonenumber = $value->phone_number;
$insert_into_user_command = "INSERT INTO user (username) VALUES ('$phonenumber')";
$insert_into_user_table = mysqli_query($con,$insert_into_user_command);
}
?>