我有一个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;
}
}
}
?>
2条答案
按热度按时间gz5pxeao1#
如果我不明白这一点,应该是:
编辑:
与@草莓一致的更好的解决方案:
yshpjwxd2#
试试这个办法。让我知道它是否有效。