我很困惑......我遇到了一个让我发疯的小问题。我的数组中有数组,我想将其插入到 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 MySQLif (empty($_POST['training'])){}else { $sql = "INSERT INTO user_resume (Diplome,Institut) VALUES (?,?)"; $stmt= $pdo->prepare($sql); $stmt->execute($_POST['training']);}
1 回答
catspeake
TA贡献1111条经验 获得超0个赞
您需要循环多维数组并使用命名占位符以使值匹配。
$sql = "INSERT INTO user_resume (Diplome, Institut) VALUES (:diploma, :institut)";
$stmt= $pdo->prepare($sql);
foreach($_POST['training'] as $params){
$stmt->execute($params);
}
- 1 回答
- 0 关注
- 87 浏览
添加回答
举报
0/150
提交
取消