有人可以解释我如何修改此代码,这样我就可以插入多个值而不只是第一个。
$aid = $_SESSION['assignmentid']; //Assignment id
$query = "INSERT INTO studentgrade (studentid, assignmentid, studentpoints) values (:studentid, '$aid' , :studentpoints)";
$query_params = array(':studentpoints' => $_POST['studentpoints'], ':studentid' => $_POST['studentid']);
$points = null;
$studentids = null;
try
{
$carPrices = array_combine($_POST['studentpoints'], $_POST['studentid']);
$stmt = $db->prepare($query);
$stmt->bindParam(':studentid', $studentids);
$stmt->bindParam(':studentpoints', $points);
foreach ($carPrices as $points => $studentids) {
$stmt->execute();
}
}
catch(PDOException $ex)
{
die("Error 1 " . $ex->getMessage());
}
答案 0 :(得分:0)
$aid = $_SESSION['assignmentid'];
$query = "INSERT INTO studentgrade (studentid, assignmentid, studentpoints) values ";
$carPrices = array_combine($_POST['studentpoints'], $_POST['studentid']);
$v = array();
foreach($carPrices as $a => $b) {
$v[] = "('$a', '$aid', '$b')";
}
$query .= implode(', ', $v);
$ query将" INSERT INTO studentgrade(studentid,assignmentid,studentpoints)值(point1,aid,id1),(point2,aid,id2),[..."