如何将表上显示的所有字段插入mysql数据库表

时间:2018-10-27 19:57:39

标签: php html

请给我备货,不知道该怎么办。我是新手,需要帮助

此查询从我数据库数据库课程表中存储的信息的后端获取数据,并将其与从studentbio表单插入到studentbio表中的数据进行匹配。

<?php
global $nwsq, $row;

$Mat = $_SESSION["MatriculationNo"];

$nwsq = $con->query("SELECT schoolcourse.CourseCode, CourseTittle, CourseUnit FROM schoolcourse, studentbio WHERE studentbio.MatriculationNo = '{$Mat}' AND studentbio.Department = schoolcourse.Deparment AND studentbio.Level = schoolcourse.Level");
?>

现在下面的代码保存了我想插入数据库中课程注册表中的表格输入

<div class="container">
<div class="single">
<div class="form-container">
<h3 class="w3ls-title w3ls-title1">Course Registeration Form</h3>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">

<div class="row">
<div class="form-group col-md-12">
<label class="col-md-3 control-lable" for="firstName">Surname</label>
<div class="col-md-9">
<input type="text" name="Surname" path="firstName" id="firstName" class="form-control input-sm" value="" required="" />
</div>
</div>
</div>

<div class="row">
<div class="form-group col-md-12">
<label class="col-md-3 control-lable" for="lastName">Other Names</label>
<div class="col-md-9">
<input type="text" name="Othernames" path="lastName" id="lastName" class="form-control input-sm" value="" required="" />
</div>
</div>
</div>

<div class="row">
<div class="form-group col-md-12">
<label class="col-md-3 control-lable" for="lastName">Matriculation Number</label>
<div class="col-md-9">
<input type="text" name="Matno" path="lastName" id="Mat" class="form-control input-sm" value="" required="" />
</div>
</div>
</div>

此部分使用while循环在$ nwsq选择查询的已回显HTML表格标签上显示结果

<div class="row">
<div class="form-group col-md-12">
<div class="col-md-9">
<?php
if ($nwsq) {
echo "<table><tr><th>CourseCode</th>
<th>CourseTittle</th>
<th>CourseUnit</th>
</tr>";

// output data of each row

while($row = $nwsq->fetch_assoc()) {

echo "<tr><td>".$row["CourseCode"]."</td>
<td>" . $row["CourseTittle"]. "</td>
<td>" . $row["CourseUnit"]. "</td>
</tr>";
}

?>
</div>
</div>
</div>
<input type="button" name="Create" value="RegisterCourse" />
</div>
</div>
</div>
</form>

这是我的问题;我如何编写一个插入查询,以将while循环显示的数据插入到mysql数据库的表中。我的代码是这样的

<?php

if(isset($_POST['Create'])){

$FName = $_POST['Surname'];
$OName = $_POST['Othernames'];
$Mat = $_POST['Matno'];
//what do I do to get the td of whatever is displayed on CourseCode, CourseTittle, CourseUnit and pass them to a variable to hold each array

//My insert query is like this
//The $con variable holds mysqli connection to my db

$statmt = $con->query("INSERT INTO courseregistrationtable (Surname, Othername, MatriculationNo, CourseCode, CourseTittle, CourseUnit) Values('{$FName}', '{$OName}', '{$Mat}', '{$VariableForCourseCode}', '{$VariableForCourseTittle}', '{$VariableForCourseUnit}')");

}
?>

我已经尝试过编写这样的插入代码,但是此代码未向数据库中插入任何内容

<?php
if (isset($_POST['Create'])) {

$temp_array = array();
while($row = $nwsq->fetch_array()){

$temp_array[] = array('CourseCode'=>$row['CourseCode'], 'CourseTittle'=>$row['CourseTittle'], 'CourseUnit'=>$row['CourseUnit']);
}

foreach($temp_array as $arr){
foreach($arr as $key=>$val){   

if($key == "CourseCode"){ $CourseCode = $val; }
If($key == "CourseTittle"){$CourseTittle = $val;}
If($key == "CourseUnit"){ $CourseUnit = $val; }
$Teller = $_POST['TellerId'];
$Mat   = $_POST['Matno'];

$Stmt = "INSERT INTO courseregistration (TellerId, MatriculationNo, CourseCode, CourseTittle, CourseUnit) VALUES ($Teller, $Mat, $CourseCode, $CourseTittle, $CourseUnit )";

$sq = $con->query($Stmt);  
}
}
}?>

请我真的需要帮助。

0 个答案:

没有答案