-1

我似乎无法弄清楚,但我的 php/mysqli 中出现错误,说明:

警告:在第 28 行的 /.../ 中为 foreach() 提供的参数无效

我的问题是如何修复上述警告,以便我可以循环插入以便能够将所有插入提供到数据库中:

<?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"));

}

        ?>
4

2 回答 2

1

您的条件 where $studentidis set 可以将值设置为空字符串。在这种情况下,您可能应该有一些条件,甚至不尝试准备 as 语句并插入数据。

于 2013-01-10T23:48:07.087 回答
0

当您在以下位置设置学生 ID 的值时:

$studentid = (isset($_POST['addtextarea'])) ? $_POST['addtextarea'] : ''; 

那不是一个数组,只是分配一个值。

您可以做的是将它设置在一个数组中,然后在初始化后将值输入该数组。前任:

$studentid = array();
$studentid[] = (isset($_POST['addtextarea'])) ? $_POST['addtextarea'] : '';    
于 2013-01-10T23:49:25.370 回答