我的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());
答案 0 :(得分:1)
在每个语句后使用var_dump($pdo->errorInfo())
AND $stm->errorInfo();
来查看PDO提供的错误。
P.S。您的查询中有错误。你错过了最后一个括号:
$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";
答案 1 :(得分:1)
我无法发布基于safeMysql的解决方案
必须进行的唯一更改是:表单字段名称必须与数据库列名称匹配(即moduleCode
必须在表单中为module_id
。解决这个问题后,您可以删除90%的代码,使其变干(代表不要重复自己)。
第一个插入的代码将与以下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为您构建查询将帮助您避免在手动创建的查询或保留字冲突或其他任何内容中allocation
和notes
之间缺少逗号等令人遗憾的错误