且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-26 10:37:22

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 =