如何将表中显示的所有字段插入到mysql数据库表中 [英] How do I insert all fields displayed on a table into mysql database table

查看:100
本文介绍了如何将表中显示的所有字段插入到mysql数据库表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请我放养,不知道该怎么做。我是新手,需要帮助



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



 <?php  
global $ nwsq, $ row ;

$ Mat = $ _SESSION [ MatriculationNo];

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





现在,下面的代码中包含表单输入我想在我的数据库中插入课程注册表



 <   div     class   = 容器 >  
< div class = single >
< div class = form-container >
< ; h3 class < span class =code-keyword> = w3ls-title w3ls-title1 > 课程注册表< / h3 >
< 表格 action = <?php echo htmlspecialchars( $ _SERVER [ PHP_SELF]);? > method =post >

< div class = row >
< div class = form-group col-md-12 >
< ; label class < span class =code-keyword> = col-md-3 control-lable for = firstName > 姓氏< / label >
< div class = col-md-9 >
< 输入 type = text name = path = firstName id = firstName class = form-control input-sm value = required = / >
< / div >
< / div >
< / div >

< div class = >
< div class = form-group col-md-12 >
< label class = col-md-3 control-lable < SP <= span> = -keyword> lastName > 其他名称< / label >
< div class = col-md-9 >
< 输入 type = text name = Othern ames path = lastName id = lastName class = form-control input-sm value = 必需 = < span class =code-keyword> / >
< / div >
< / div >
< / div >

< div class = row >
< div class = form-group col-md-12 >
< label class = col- md-3 control-lable for = lastName > 入学编号 < / label >
< div class = col-md-9 >
< 输入 类型 = text name = Matno path = lastName id = Mat class = form-control input-sm value = 必需 = / >
< / 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>;

//每行输出数据

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

echo< tr>< td>。$ row [CourseCode]。< / td>
< td> 。 $行[ CourseTittle。 < / td>
< td> 。 $行[ CourseUnit。 < / td>
< / tr>;

}

?>
< / div>
< / div>
< / div>
< input type =buttonname =Createvalue =RegisterCourse/>
< / div>
< / div>
< / div>
< / form>





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

<?php 

if(isset($ _ POST ['Create'])){

$ FName = $ _POST ['Surname'];
$ OName = $ _POST ['Othernames'];
$ Mat = $ _POST ['Matno'];
//如何获取CourseCode,CourseTittle,CourseUnit上显示的内容的td,并将​​它们传递给变量以保存每个数组

//我的插入查询是这样的
// $ con变量保存与我的db

$ statmt = $ con>查询的mysqli连接(INSERT INTO courseregistrationtable(Surname,Othername,MatriculationNo,CourseCode,CourseTittle,CourseUnit) )值('{$ 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']);
}

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

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

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

$ sq = $ con>查询($ Stmt);
}
}
}?>

解决方案

nwsq,

;


Mat =


Please i am stocked and don't know what to do. I am new and need help

This query Fetch data from a back end of stored information on my database schoolcourse table matching it against data inserted from the studentbio form into the studentbio table.

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



Now the code below holds the form input i want to insert into a course registration table on my db

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



This part displays the result on an echoed HTML table tag from the $nwsq select query using while loop

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



Here is my major problem; How do i write an insert query to insert this data being displayed by the while loop into a table on mysql database. My code is like this

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

}
?>



What I have tried:

I have tried writing my insert code like this but this code is not inserting anything to my db

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

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

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

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

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

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

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

解决方案

nwsq,


row;


Mat =


这篇关于如何将表中显示的所有字段插入到mysql数据库表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆