我做了这种在数据库中添加数据的形式。我的考试表包含exam_code(PK),exam_title和subject_code(FK)。这是设计
<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>
我的查询在这里是否正确?无论如何我都想不到插入数据。这里..
<?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>");
}
}
?>
答案 0 :(得分:0)
更改您的PHP代码 来自
<?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>";
}
?>
到此
<?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>";
}
?>
答案 1 :(得分:0)
将值存储在Variable中,然后将其存储起来。
<?php
include('db.php');
$subj = $connect->query("SELECT subject_code FROM subject");
while($row1 = mysqli_fetch_array($subj)){
$subjectCode = $row1[subject_code];
echo "<option value = $subjectCode>$subjectCode</option>";
}
?>
它的作品。
答案 2 :(得分:0)
我通过添加禁用外键的查询来修复它。
$set = $connect->query('SET foreign_key_checks = 0');
/*insert query*/
$set1 = $connect->query('SET foreign_key_checks = 1');