PDO 在数组中插入数组

我很困惑......我遇到了一个让我发疯的小问题。我的数组中有数组,我想将其插入到 SQL 表中。我的问题是我不知道如何在数组中插入数组......


数组: Array ( [1] => Array ( [diploma] => Master [institute] => IAE ) [2] => Array ( [diploma] => Bachelor [institute] => University ) )


欢迎任何建议:)非常感谢法国!


<input id="mytext-{cid}" type="text" name="training[{cid}][diploma]" placeholder="Diplôme" value="">

<input name="training[{cid}][institut]" placeholder="Institut">


 try

{

$pdo = new PDO('mysql:host='.$host.';dbname='.$bd, $login, $password);

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

}

catch (Exception $e) //Le catch est chargé d’intercepter une éventuelle erreur

{

die ($e->getMessage());

}

global $pdo;


// INSERT MySQL


if (empty($_POST['training'])){


}

else {      

    $sql = "INSERT INTO user_resume (Diplome,Institut) VALUES (?,?)";

    $stmt= $pdo->prepare($sql);

    $stmt->execute($_POST['training']);

}      


扬帆大鱼
浏览 88回答 1
1回答

catspeake

您需要循环多维数组并使用命名占位符以使值匹配。$sql = "INSERT INTO user_resume (Diplome, Institut) VALUES (:diploma, :institut)";$stmt= $pdo->prepare($sql);foreach($_POST['training'] as $params){&nbsp; &nbsp; $stmt->execute($params);}
打开App,查看更多内容
随时随地看视频慕课网APP