将来自动态填充表的数据插入mysql数据库

时间:2018-10-25 11:08:47

标签: php

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

此查询从我的数据库学校课程表中获取数据     

$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循环显示选择查询的结果

<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 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}')");

}
?>

1 个答案:

答案 0 :(得分:-1)

这是从一个表中获取值并将其放入另一个表中的方法。

 $temp_array = array(); //initialize array   
 while($result = mysql_fetch_array($rs)) { //your current while loop
       echo '<tr>
             <td>'.$result["forename"].'</td>
             <td>'.$result["surname"].'</td>
             </tr>';
       $temp_array[] = array('forename=>$result['forename'], 'surname'=>$result['surname']);
 }

然后,在while循环之后,您可以执行以下操作:

foreach($temp_array as $arr){
     foreach($arr as $key=>$val){
        if($key == "forename"){ $forename = $val; }
        if($key == "surname") { $surname = $val; }
        $sql = "INSERT INTO sometable (forename, surname) VALUES ($forename, 
                $surname)";
        //execute query as you normally would. Note, the above query is a
        //sample, you'll probably need some other identifier in there also 
        //which I'm not aware of based on your code.

     }
 }