【发布时间】:2015-06-11 22:56:58
【问题描述】:
我正在尝试将多个复选框的结果保存为单独的记录。我的代码不起作用。请帮忙!
<?php
session_start();
$id = $_SESSION['user_id'];
$db = new PDO('mysql:host=localhost;dbname=idp;charset=utf8','root', '');
foreach($_POST['comp'] as $val){
$tmp['user_id'] = $id;
$tmp['comp_id'] = $val;
$vars[] = $tmp;
}
$qry = "INSERT INTO compentency_result (user_id, result) VALUES (:user_id, :comp_id)";
try
{
$sql = $db->prepare($qry);
$numRows = 0;
foreach($vars as $insert){
$numRows += $sql->execute($insert);
}
print("<p>There were {$numRows} inserted into the database!</p>");
}
catch(PDOException $e)
{
print("<p>Oops! There was an issue - this is the message: {$e->getMessage()}</p>");
}
?>
结果显示数据库中没有添加任何内容。
【问题讨论】:
-
你没有绑定任何东西。
-
我戒掉了会绑定 Ralph 的东西。,但你应该始终绑定你的查询值,对 @Fred-ii-
-
你是 Sam - @JayBlanchard
-
您收集受影响的行数的方式有点奇怪,因为您期望“true”返回“1”。这里的行为可能不是那么一致。
-
请问,你建议我如何解决这个问题?对 PDO 非常陌生。。谢谢