编辑:我发布了a new question(而不是编辑),因为我尝试的内容和代码非常冗长,并且意味着完全取代了这个问题。
我正在使用PDO INSERT
多行进入SQLite表,但是我在使用绑定参数执行查询时遇到了问题。我有三个模式表:
列schoolB_equivalencies:
schoolA_courses的列:
schoolB_courses的列:
我正在尝试使用prepare()
,值数组和子查询一次进行多次插入。一旦运行INSERT
语句(插入一行):
$dir = 'sqlite:/path/to/courses.sqlite3';
$dbh = new PDO($dir) or die("error");
$stmt = $dbh->prepare('INSERT INTO schoolB_equivalencies (schoolA_id, schoolB_id, is_archived) VALUES ( (SELECT id from schoolA_courses WHERE course_number="103" AND course_prefix="RPED"), (SELECT id from schoolB_courses WHERE course_number="251" AND course_prefix="ARCH"),0)');
$stmt->execute();
但是,当我使用绑定参数和值数组合并相同的语句时,不会插入任何行:
$dir = 'sqlite:/path/to/courses.sqlite3';
$dbh = new PDO($dir) or die("error");
$selections = array(
0 => array(
0 => "RPED", // THE schoolA COURSE PREFIX
1 => "103", //THE schoolA COURSE NUMBER
2 => "ARCH", //THE schoolB COURSE PREFIX
3 => "251" //THE schoolB COURSE NUMBER
),
1 => array(
0 => "RPED", // THE schoolA COURSE PREFIX
1 => "126", //THE schoolA COURSE NUMBER
2 => "ARCH", //THE schoolB COURSE PREFIX
3 => "261" //THE schoolB COURSE NUMBER
)
);
$stmt = $dbh->prepare('INSERT INTO schoolB_equivalencies (schoolA_id, schoolB_id, is_archived) VALUES ( (SELECT id from schoolA_courses WHERE course_number=:schoolA_code AND course_prefix=:schoolA_prefix), (SELECT id from schoolB_courses WHERE course_number=:schoolB_code AND course_prefix=:schoolB_prefix),0)');
foreach ( $selections as $selection ):
$stmt->bindParam(':schoolA_number', $selection[1]);
$stmt->bindParam(':schoolA_prefix', $selection[0]);
$stmt->bindParam(':schoolB_number', $selection[3]);
$stmt->bindParam(':schoolB_prefix', $selection[2]);
$stmt->execute();
print_r($dbh->errorInfo());
endforeach;
error_info()
的输出在两个插入上显示代码0000,这似乎是“成功”指示符,但没有插入任何行。基于单INSERT
工作且error_info()
报告成功的事实,我猜测我的绑定参数有什么问题。
答案 0 :(得分:0)
首先......不应该是
$stmt->bindParam(':schoolA_number', $selection[0][1]);
$stmt->bindParam(':schoolA_prefix', $selection[0][0]);
$stmt->bindParam(':schoolB_number', $selection[0][3]);
$stmt->bindParam(':schoolB_prefix', $selection[0][2]);
OR
$stmt->bindParam(':schoolA_number', $selection[1][1]);
$stmt->bindParam(':schoolA_prefix', $selection[1][0]);
$stmt->bindParam(':schoolB_number', $selection[1][3]);
$stmt->bindParam(':schoolB_prefix', $selection[1][2]);
OR
foreach ( $selections as $selection ) {
$stmt = $dbh->prepare('INSERT INTO schoolB_equivalencies (schoolA_id, schoolB_id, is_archived) VALUES ( (SELECT id from schoolA_courses WHERE course_number=:schoolA_code AND course_prefix=:schoolA_prefix), (SELECT id from schoolB_courses WHERE course_number=:schoolB_code AND course_prefix=:schoolB_prefix),0)');
$stmt->bindParam(':schoolA_number', $selection[1]);
$stmt->bindParam(':schoolA_prefix', $selection[0]);
$stmt->bindParam(':schoolB_number', $selection[3]);
$stmt->bindParam(':schoolB_prefix', $selection[2]);
$stmt->execute();
print_r($dbh->errorInfo());
}
AND NOT
foreach ( $selections as $selection ) {
$stmt->bindParam(':schoolA_number', $selection[1]);
$stmt->bindParam(':schoolA_prefix', $selection[0]);
$stmt->bindParam(':schoolB_number', $selection[3]);
$stmt->bindParam(':schoolB_prefix', $selection[2]);
$stmt->execute();
...
}