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 に答える