数组到mysql php

ymdaylpp  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(314)

我有一个json字符串,它包含两个对象,人名和它们关联的电话号码,如下所示(姓名和电话的长度始终相同):

{"Name":["Name One","Name Two","Name Three"],"Phone":["123465789","3216546564","15641654654"]}

我正试图通过php将其插入mysql数据库,但在如何将这两个值插入同一个循环中有点困难。
目前php代码如下:

<?php

$dbuser = "";
$dbpassword = "";
$database = "";

$result;
$resultSuccess;

$user = $_POST['user'];
$proc_name = $_POST['proc_name'];
$person_name_contacts = $_POST['person_name_contacts'];

$mysqli = new mysqli("localhost", $dbuser, $dbpassword, $database);
if ($mysqli->connect_errno) {
$result = 'Database Connection Failed';
}else{

$user = mysqli_real_escape_string($mysqli,$user);
$proc_name = mysqli_real_escape_string($mysqli,$proc_name);
$person_name_contacts = 
mysqli_real_escape_string($mysqli,$person_name_contacts);

$person_name_contacts=json_decode($person_name_contacts);

foreach($person_name_contacts as $value) {
    $sql = "INSERT INTO evac_people (user, evac_procedure, person_name,  
person_contact)
        VALUES     ('".$user."','".$proc_name."','".$person_names."','".$person_contacts."')";
    $result = $mysqli->query($sql);

    if($mysqli->error === ""){
        echo "true";
    }else{
        echo $mysqli->error;
    }
}

}
?>
gz5pxeao

gz5pxeao1#

如果我不明白这一点,应该是:

foreach ($person_name_contacts['Name'] as $k =>$value) {
    $person_name = $value;
    $person_contact = $person_name_contacts['Phone'][$k];

    $sql = "INSERT INTO evac_people (user, evac_procedure, person_name,  person_contact)
            VALUES     ('" . $user . "','" . $proc_name . "','" . $person_name . "','" . $person_contact . "')";
    $result = $mysqli->query($sql);

    if ($mysqli->error === "") {
        echo "true";
    } else {
        echo $mysqli->error;
    }
}

编辑:
与@草莓一致的更好的解决方案:

$sql = "INSERT INTO evac_people (user, evac_procedure, person_name,  person_contact) VALUES ";
$sqlValue = array();
foreach ($person_name_contacts['Name'] as $k =>$value) {
    $person_names = $value;
    $person_contacts = $person_name_contacts['Phone'][$k];
    $sqlValue[] = "('" . $user . "','" . $proc_name . "','" . $person_names . "','" . $person_contacts . "')";
}
if (!empty($sqlValue)) {
    $sql = $sql . implode(',', $sqlValue);
    $result = $mysqli->query($sql);
    if ($mysqli->error === "") {
        echo "true";
    } else {
        echo $mysqli->error;
    }
}
yshpjwxd

yshpjwxd2#

试试这个办法。让我知道它是否有效。

$json = '{"Name":["Name One","Name Two","Name Three"],"Phone":["123465789","3216546564","15641654654"]}';

    $result = json_decode ($json);

    $array = array();

    foreach ($result->Name as $k =>$value)
    {
        $name = $value;
        $contact = $result->Phone[$k];

    $sql = "INSERT INTO evac_people (user, evac_procedure, person_name,  person_contact)
             VALUES     ('".$user."','".$proc_name."','".$name."','". $contact . "')";
    $result = $mysqli->query($sql);

    if ($mysqli->error === "") {
        echo "true";
    } else {
        echo $mysqli->error;
    }

    }

相关问题