Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何执行循环以插入每个学生的详细信息_Php_Mysqli - Fatal编程技术网

Php 如何执行循环以插入每个学生的详细信息

Php 如何执行循环以插入每个学生的详细信息,php,mysqli,Php,Mysqli,下面我想在数据库中插入值。我想将每个学生插入所选的课程(考试) 现在让我们假设所选的课程是34,我的问题是假设一个选择框包含多个学生,他们将进行以下评估: <select name='addtextarea' id='studentadd' multiple='multiple' size='10'> <option value='1'>Joe Blogs</option> <option value='4'>Bill Kite</opti

下面我想在数据库中插入值。我想将每个学生插入所选的课程(考试)

现在让我们假设所选的课程是
34
,我的问题是假设一个选择框包含多个学生,他们将进行以下评估:

<select name='addtextarea' id='studentadd'  multiple='multiple' size='10'>
<option value='1'>Joe Blogs</option>
<option value='4'>Bill Kite</option>
<option value='6'>Mary Scott</option>
</select>
下面是使用mysqli/php插入的代码:

$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
        }                                           

        $insert->bind_param("ii", $sessionid, $studentid);

        $insert->execute();

        if ($insert->errno) {
        // Handle query error here
        }

        $insert->close();

只需循环
bind_param()
execute()
,只需准备一次:

为了避免插入零,我还将整个准备好的语句包装成如下内容:

if(isset($_POST['addtextarea']) && is_array($_POST['addtextarea']) && count($_POST['addtextarea']) > 0 && isset($_POST['Idcurrent']))
{
    // we have some data to insert, do the prepared stmt

    $insertsql = "
    INSERT INTO Student_Session
    (SessionId, StudentId)
    VALUES
    (?, ?)
    ";

    if ($insert = $mysqli->prepare($insertsql)) {

        foreach($_POST['addtextarea'] as $studentid)
        {
            $insert->bind_param("ii", $_POST['Idcurrent'], $studentid);

            $insert->execute();

            if ($insert->errno) {
                // Handle query error here
            }
        }

        $insert->close();
    }
    else
    {
        // prepare() call failed - syntax error in query or error with the db
        // Handle errors with prepare operation here
    }
}
else
{
    // post data is missing, show message
}

您好,我可以这么快回答您的问题吗?因为您在if语句末尾调用了
isset($\u POST['Idcurrent
]),这是否意味着
$sessionid
现在应该是
$sessionid$\u POST['Idcurrent']:'?这是在长if语句中还是在它之外?@user1964964查看我的编辑-我已经重新构造了它。我还没有创建$sessionid变量,因为它只在我刚从文章中获取时使用过一次。
if(isset($_POST['addtextarea']) && is_array($_POST['addtextarea']) && count($_POST['addtextarea']) > 0 && isset($_POST['Idcurrent']))
{
    // we have some data to insert, do the prepared stmt

    $insertsql = "
    INSERT INTO Student_Session
    (SessionId, StudentId)
    VALUES
    (?, ?)
    ";

    if ($insert = $mysqli->prepare($insertsql)) {

        foreach($_POST['addtextarea'] as $studentid)
        {
            $insert->bind_param("ii", $_POST['Idcurrent'], $studentid);

            $insert->execute();

            if ($insert->errno) {
                // Handle query error here
            }
        }

        $insert->close();
    }
    else
    {
        // prepare() call failed - syntax error in query or error with the db
        // Handle errors with prepare operation here
    }
}
else
{
    // post data is missing, show message
}