i made this form of adding data in the database. I have exam table that contains exam_code(PK), exam_title and subject_code(FK). Here is the design
<div style="width:800px;height:auto;margin-left:auto;margin-right:auto;margin-top:50px;">
<form action="" method="POST" class="form-horizontal" role="form">
<div class="form-group">
<div class="col-xs-6 col-sm-3 ">
<input name="code" type="text" class="form-control" id="excode" placeholder="Enter Exam Code">
</div>
<div class="col-xs-6 col-sm-3 ">
<input name="title" type="text" class="form-control" id="extitle" placeholder="Enter Exam Title">
</div>
<div class="col-xs-6 col-sm-3 ">
<select name="subjcode" class="form-control">
<option selected="selected">Choose subject</option>
<option disabled="disabled">---------------------------------</option>
<?php
include('db.php');
$subj = $connect->query("SELECT subject_code FROM subject");
while($row1 = mysqli_fetch_array($subj)){
echo "<option value = $row1[subject_code]>$row1[subject_code]</option>";
}
?>
</select>
</div>
<div class="col-xs-6 col-sm-3 ">
<input type="submit" name="add" class="btn btn-default" value="Add" />
</div>
</div>
</form>
</div>
Is my query here correct? I can't think of anyway to insert the data. Here..
<?php
include('db.php');
if(isset($_POST['add'])){
$excode = $_POST['code'];
$extitle = $_POST['title'];
$subcode = $_POST['subjcode'];
$examinsert = $connect->query("INSERT INTO exam (exam_code, exam_title, subject_code) VALUES ('$excode', '$extitle', '$subcode')");
if(!$examinsert){
die("<script>
alert('Error encountered, Reloading page');
window.location.href='teacher.php';
</script>");
}else{
die("<script>
alert('Your exam title has been added. You will see your titles in the Examination title section below!');
window.location.href='teacher.php';
</script>");
}
}
?>