我似乎無法推測出來,但我得到一個錯誤在我的PHP /庫MySQLi指出:)中提供的foreach無效參數(:foreach()循環導致PHP/mysqli的錯誤
警告 /.../第28行
我的問題是,如何才能將上述警告是固定的,這樣我可以遍歷插入到能夠提供所有插入到數據庫:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
// connect to the database
include('connect.php');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}
$studentid = (isset($_POST['addtextarea'])) ? $_POST['addtextarea'] : '';
$sessionid = (isset($_POST['Idcurrent'])) ? $_POST['Idcurrent'] : '';
$insertsql = "
INSERT INTO Student_Session
(SessionId, StudentId)
VALUES
(?, ?)
";
if (!$insert = $mysqli->prepare($insertsql)) {
// Handle errors with prepare operation here
}
foreach($studentid as $id)
{
$insert->bind_param("ii", $sessionid, $id);
$insert->execute();
if ($insert->errno) {
// Handle query error here
}
}
$insert->close();
$query = "SELECT ss.SessionId, SessionName, StudentId
FROM
Student_Session ss
INNER JOIN Session s ON
ss.SessionId = s.SessionId
WHERE ss.SessionId = ? AND StudentId = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("ii", $sessionid, $studentid);
// execute query
$stmt->execute();
// get result and assign variables (prefix with db)
$stmt->bind_result($dbSessionId, $dbSessionName, $dbStudentId);
//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();
//fetch the results
$stmt->fetch();
if ($numrows == 1){
echo json_encode(array('errorflag'=>false,'msg'=>"Students have been successfully added into the Assessment"));
}else{
echo json_encode(array('errorflag'=>true,'msg'=>"An error has occured, Students have not been added into the Assessment"));
}
?>
是$ studentid數組嗎?如果沒有,它會拋出該錯誤。試試$ studentid的var_dump。 –
嘗試轉儲var_dump/print_r($ studentid),檢查它是否被創建爲一個數組。事實上,看着你的代碼,它不僅僅是其中一個後置字段的價值。 – Mark
另外我想知道您是否將$ studentid設置爲單個值?如果是這樣,你爲什麼需要使用數組格式? –