0

我的 SQL PDO INSERT 查询似乎没有将数据插入表中,并且没有返回任何错误消息说明它为什么不能这样做。关于发生了什么的任何想法?

更新:array(3) { [0]=> string(5) "00000" [1]=> NULL [2]=> NULL }运行后我收到以下错误消息var_dump($pdo->errorInfo())

$dept = $_POST['dept'];
$module_id = $_POST['moduleCode'];
$day_id = $_POST['day'];
$period_id = $_POST['period'];
$length_id = $_POST['length'];
$semester = $_POST['semester'];
$round = $_POST['round'];
$group_size = $_POST['group_size'];
$room_structure = $_POST['room_structure'];
$lecture_style = $_POST['lecture_style'];
$roomAllocation = $_POST['roomAllocation'];
$notes = $_POST['notes'];

// insert request into table

$sql = "INSERT INTO ts_request
                (dept_id,
                module_id,
                day_id,
                period_id,
                length_id,
                semester,
                round,
                group_size,
                room_structure,
                lecture_style,
                park_id,
                allocation
                notes)
                VALUES 
                (:dept,
                :module_id,
                :day_id,
                :period_id,
                :length_id,
                :semester,
                :round,
                :group_size,
                :room_structure,
                :lecture_style,
                :park,
                :allocation
                :notes)";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':dept' => $dept, 
                                            ':module_id' => $module_id,
                                            ':day_id' => $day_id,
                                            ':period_id' => $period_id,
                                            ':length_id' => $length_id,
                                            ':semester' => $semester,
                                            ':round' => $round,     
                                            ':group_size' => $group_size,                                                                                                                                                                               
                                            ':room_structure' => $room_structure,                                                                                                                                                                               
                                            ':lecture_style' => $lecture_style, 
                                            ':park' => $park,   
                                            ':allocation' => $roomAllocation,                                                                                                                                                                                                                                                                                                                                                                                                                                           
                                            ':notes' => $notes ) );
$request_id = $pdo->lastInsertId();

// create allocation

$sql = "INSERT INTO ts_allocation
                (request_id,
                status)
                VALUES 
                (:request_id,
                :status";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':request_id' => $request_id, 
                                            ':status' => $status ) );
$allocation_id = $pdo->lastInsertId();

// insert weeks

$weeks = explode(",", $_POST["weeks"]);
for ($i = 0; $i < count($weeks); $i++) {
$sql = "INSERT INTO ts_week
                (request_id,
                allocation_id,
                week)
                VALUES 
                (:request_id,
                :allocation_id,
                :week)";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':request_id' => $request_id, 
                                            ':allocation_id' => $allocation_id,
                                            ':week' => $weeks[$i] ) );
}

// insert fac pref

$roomFac = explode(",", $_POST["roomFac"]);
for ($i = 0; $i < count($roomFac); $i++) {
$sql = "INSERT INTO ts_facpref
                (request_id,
                facilities_id)
                VALUES 
                (:request_id,
                :facilities_id)";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':request_id' => $request_id, 
                                            ':facilities_id' => $roomFac[$i] ) );
}

// insert room pref

$room_id = explode(",", $_POST["roomPref"]);
for ($i = 0; $i < count($room_id); $i++) {
$sql = "INSERT INTO ts_roompref
                (request_id,
                room_id)
                VALUES 
                (:request_id,
                :room_id)";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':request_id' => $request_id, 
                                            ':room_id' => $room_id[$i] ) );
}
var_dump($stm->errorInfo());
4

2 回答 2

1

在每个语句之后使用 var_dump($pdo->errorInfo())AND $stm->errorInfo();来查看 PDO 给出的错误。

PS您的查询中有错误。你错过了最后一个括号:

$sql = "INSERT INTO ts_allocation
                (request_id,
                status
                VALUES 
                (:request_id,
                :status";

还有这个

$sql = "INSERT INTO ts_facpref
                (request_id,
                facilities_id,
                VALUES 
                (:request_id,
                :facilities_id";
于 2013-02-18T06:59:16.050 回答
1

我无法抗拒发布基于safeMysql的解决方案。
唯一需要做的改变是:表单字段名称必须与数据库列名称匹配(即moduleCode必须module_id在表单中)。解决此问题后,您可以摆脱 90% 的代码,使其成为 DRY(代表 Don't Repeat Yourself)。
第一个插入的代码将与以下 4 行一样小:

$allowed = array(dept_id, module_id, day_id, period_id, length_id, semester, round,
            group_size, room_structure, lecture_style, park_id, allocation, notes);
$data = $db->filterArray($allowed, $_POST);
$db->query("INSERT INTO ts_request SET ?u", $data);

不用说,在如此小的代码中发现错误比在较大的代码中容易得多。
此外,让 PHP 为您构建查询将帮助您避免此类可悲的错误,例如手动创建的查询之间allocationnotes中缺少逗号或保留字冲突或其他任何错误

于 2013-02-18T07:45:55.453 回答