我想使用以下脚本插入记录数。但是,只有第一个被插入。之后,它只是停止而没有显示任何错误。这些准备好的语句执行有什么问题?
//$dbc = database connection //shortan here
// $mid[] = {1,2,3,4}; //sortened here.
$q5 = "INSERT INTO user_book_trn(user_id, member_id, book_id, date_read, lang_id) VALUES (?, ?, ?, now(), ?)";
$s5 = mysqli_prepare($dbc, $q5);
//Bind the variables:
mysqli_stmt_bind_param($s5, 'iiii', $user_id, $member_id, $book_id, $lang_id);
foreach($mid as $mk => $mv) { //check for each selected check box value from member list:
$q2 = "SELECT user_id, member_id, book_id FROM user_book_trn WHERE user_id = {$_SESSION['myuser']['userid']} and member_id = {$mv} and book_id= {$w}";
$r3 = mysqli_query($dbc, $q2);
if (mysqli_num_rows($r3) == 0) { //title is available for this user.
//Assign the values to variables:
$user_id = (int)$_SESSION['myuser']['userid'];
$member_id = (int)$mv;
$book_id = (int)$w;
$lang_id = (int)$_SESSION['lid'];
//just to check each iteration gets new values:
echo "user_id : $user_id \n";
echo "member_id : $member_id \n";
echo "book_id : $book_id \n";
//Execute the query:
mysqli_stmt_execute($s5);
if (mysqli_affected_rows($dbc) == 1) {
//this runs ok just for the first iteration.. Why?
echo "<p><b> The book $t is added. </b></p>";
$_SESSION['bookid'] = $book_id;
}
}
}