如何将每个学科的学生成绩[]插入mysql数据库

时间:2019-06-21 22:54:24

标签: php html mysql

问题-我无法使用php将每个学科的学生成绩[]插入mysql数据库。我正在从HTML表单输入中提取数据。

所以我正试图在mysql中建立一个结果表,像这样:

schid(foreign key),
studid(foreign key),
classid(foreign key),
subjid(foreign key),
marks

当前,我可以无错误地插入所有schid,studid和classid,但是不能插入HTML表单中的每个subjid和标记。

HTML代码

<!-- Subjects & marks -->
<h5 class="mt-2 mb-2">Enter Subject Marks</h5>
<?php
// Pull subjects based on schid
$sql = "SELECT DISTINCT subjid, subjname FROM subjects WHERE 
schid='$sessionschid'";

$dbSubjQuery = mysqli_query($conn, $sql);

// Start while loop
while ($dbSubjInfo = mysqli_fetch_assoc($dbSubjQuery)) {                                         
$dbSubjID = $dbSubjInfo['subjid'];
$dbSubjName = ucwords($dbSubjInfo['subjname']);                                     
?>
<div class="form-group form-row">
<label for="subjects" class="col-form-label col-3"><?php echo 
$dbSubjName;?>:</label>

<div class="col-9">
<input type="text" name="marks[]" class="form-control" value="" 
required>
</div>
</div>
<?php } // close while loop ?>

PHP代码

// Grab marks
$mark = $_POST['marks'];
foreach ($mark as $key => $value) {
    $eachmark .= $value;
}
// Create array for subjects and marks
$resultData = array(
    'schid' => $sessionschid,
    'studid' => $studrollno,
    'classid' => $studclass,
    'subjid' => $dbSubjID,
    'marks' => $eachmark,
);

foreach ($resultData as $key => $value) {
    $k[] = $key;
    $v[] = "'".$value."'";
}

$k = implode(',', $k);
$v = implode(',', $v);

// Query for insert resultData
$sql = "INSERT INTO RESULTS ($k) VALUES ($v)";
// Insert resultData
$insertRes = mysqli_query($conn, $sql);

以上代码将所有主题ID插入数据库的subjid列中,并将所有标记插入数据库的marks列中。但是..我想在每个subjectID中插入其标记。我希望我能解释清楚。感谢您的帮助。

0 个答案:

没有答案