我是php的初学者,正在设计学生信息系统,学生需要根据座位数输入9个科目及其相应的成绩。除了我一直在做的事,还有其他更好的方法吗?
这些是我的代码。
<script type="text/javascript">
function showDiv(select){
if(select.value==2){
document.getElementById('div2a').style.display = "block";
} else{
document.getElementById('div2a').style.display = "none";
}
}
</script>
<style>
.striped-border {
border: 1px solid #0d4115;
margin: auto;
margin-top: 5%;
margin-bottom: 2%;
}
.form2a{
display: none;
}
</style>
<form name="register" method="POST" action="process2.php" >
<div class="col-md-12">
<div class="form-group">
<label>Select Number of Sitting(s)</label>
<select name="sitting" id="sitting" class="form-control" onchange="showDiv(this)">
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
</div>
<div class="output1" id="div1" name="onesitting">
<div class="col-md-6">
<div class="form-group">
<label>School Name</label>
<input type="text" name="schoolname1" id="schoolname" class="form-control" placeholder="School Name" required>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Exam Type</label>
<select name="examboard1" id="examboard" class="form-control" required>
<option value="">Exam Type</option>
<option value="WAEC">WAEC(SSCE)</option>
<option value="WAEC">WAEC(GCE)</option>
<option value="NECO">NECO(SSCE)</option>
<option value="NECO">NECO(GCE)</option>
<option value="NABTEB">NABTEB(SSCE)</option>
<option value="NABTEB">NABTEB(GCE)</option>
<option value="OTHERS">OTHERS</option>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Exam Date</label>
<?php
$examyear = range(1960, 2050);
?>
<select class="form-control" name="examdate1" id="examdate" placeholder="Exam Date">
<option value="">Select date</option>
<?php
foreach($examyear as $examdate){
echo '<option value= "' .$examdate.'">'.$examdate.'</option>';
}
?>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Exam Number</label>
<input type="text" class="form-control" name="examno1" id="examno" placeholder="Exam Number">
</div>
</div>
<br>
<table width="100%" cellpadding="0" cellspacing="0" border="0" class="table table-borderless" id="example">
<tr>
<td width="12%"><label class="control-label">S/NO</label></td>
<td width="53%"><label class="control-label">SUBJECTS</label></td>
<td width="35%"><label class="control-label">GRADE</label></td>
</tr>
<tr>
<td>1</td>
<td>
<?php include("config.php");
$sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
?>
<select name="subject11" class="form-control" id="subject">
<option value="" selected="selected">Select subject</option>
<?php
if($count > 0){
while($row = mysqli_fetch_assoc($query)){
$subject_id = $row['subject_id'];
$subject_name = $row['subject_name'];
echo "<option>" . $row['subject_name'] . "</option>";
}
}else{
echo "<option value = ''>Subject not available</option>";
}
?>
</select>
</td>
<td>
<select name="grade11" class="form-control">
<option value=""> Select</option>
<option value="A1">A1</option>
<option value="B2">B2</option>
<option value="B3">B3</option>
<option value="C4">C4</option>
<option value="C5">C5</option>
<option value="C6">C6</option>
<option value="D7">D7</option>
<option value="E8">E8</option>
<option value="F9">F9</option> </select>
</td>
</tr>
<tr>
<td>2</td>
<td>
<?php include("config.php");
$sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
?>
<select name="subject12" class="form-control" id="subject">
<option value="" selected="selected">Select subject</option>
<?php
if($count > 0){
while($row = mysqli_fetch_assoc($query)){
$subject_id = $row['subject_id'];
$subject_name = $row['subject_name'];
echo "<option>" . $row['subject_name'] . "</option>";
}
}else{
echo "<option value = ''>Subject not available</option>";
}
?>
</select>
</td>
<td>
<select name="grade12" class="form-control">
<option value=""> Select </option>
<option value="A1">A1</option>
<option value="B2">B2</option>
<option value="B3">B3</option>
<option value="C4">C4</option>
<option value="C5">C5</option>
<option value="C6">C6</option>
<option value="D7">D7</option>
<option value="E8">E8</option>
<option value="F9">F9</option> </select>
</td>
</tr>
<tr>
<td>3</td>
<td>
<?php include("config.php");
$sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
?>
<select name="subject13" class="form-control" id="subject">
<option value="" selected="selected">Select subject</option>
<?php
if($count > 0){
while($row = mysqli_fetch_assoc($query)){
$subject_id = $row['subject_id'];
$subject_name = $row['subject_name'];
echo "<option>" . $row['subject_name'] . "</option>";
}
}else{
echo "<option value = ''>Subject not available</option>";
}
?>
</select>
</td>
<td>
<select name="grade13" class="form-control">
<option value=""> Select </option>
<option value="A1">A1</option>
<option value="B2">B2</option>
<option value="B3">B3</option>
<option value="C4">C4</option>
<option value="C5">C5</option>
<option value="C6">C6</option>
<option value="D7">D7</option>
<option value="E8">E8</option>
<option value="F9">F9</option> </select>
</td>
</tr>
<tr>
<td>4</td>
<td>
<?php include("config.php");
$sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
?>
<select name="subject14" class="form-control" id="subject">
<option value="" selected="selected">Select subject</option>
<?php
if($count > 0){
while($row = mysqli_fetch_assoc($query)){
$subject_id = $row['subject_id'];
$subject_name = $row['subject_name'];
echo "<option>" . $row['subject_name'] . "</option>";
}
}else{
echo "<option value = ''>Subject not available</option>";
}
?>
</select>
</td>
<td>
<select name="grade14" class="form-control">
<option value=""> Select </option>
<option value="A1">A1</option>
<option value="B2">B2</option>
<option value="B3">B3</option>
<option value="C4">C4</option>
<option value="C5">C5</option>
<option value="C6">C6</option>
<option value="D7">D7</option>
<option value="E8">E8</option>
<option value="F9">F9</option> </select>
</td>
</tr>
<tr>
<td>5</td>
<td>
<?php include("config.php");
$sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
?>
<select name="subject15" class="form-control" id="subject">
<option value="" selected="selected">Select subject</option>
<?php
if($count > 0){
while($row = mysqli_fetch_assoc($query)){
$subject_id = $row['subject_id'];
$subject_name = $row['subject_name'];
echo "<option>" . $row['subject_name'] . "</option>";
}
}else{
echo "<option value = ''>Subject not available</option>";
}
?>
</select>
</td>
<td>
<select name="grade15" class="form-control">
<option value="">Select </option>
<option value="A1">A1</option>
<option value="B2">B2</option>
<option value="B3">B3</option>
<option value="C4">C4</option>
<option value="C5">C5</option>
<option value="C6">C6</option>
<option value="D7">D7</option>
<option value="E8">E8</option>
<option value="F9">F9</option> </select>
</td>
</tr>
</table>
<br>
</div>
<div class="form2a" id="div2a" name="twositting">
<br>
<h4 style="text-align:center; margin-top: 0px; margin-bottom: 5%; background-color:#0d4115; width: auto; color: white;"> SECOND SITTING</h4>
<div class="col-md-6">
<div class="form-group">
<label>School Name</label>
<input type="text" name="schoolname2" id="schoolname" class="form-control" placeholder="School Name" required>
<span id="error_schoolname" class="text-danger"></span>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Exam Type</label>
<select name="examboard2" id="examboard" class="form-control" required>
<option value="">Exam Type</option>
<option value="WAEC">WAEC(SSCE)</option>
<option value="WAEC">WAEC(GCE)</option>
<option value="NECO">NECO(SSCE)</option>
<option value="NECO">NECO(GCE)</option>
<option value="NABTEB">NABTEB(SSCE)</option>
<option value="NABTEB">NABTEB(GCE)</option>
<option value="OTHERS">OTHERS</option>
<span id="error_examboard" class="text-danger"></span>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Exam Date</label>
<?php
$examyear = range(1960, 2099);
?>
<select class="form-control" name="examdate2" id="examdate" placeholder="Exam Date">
<option value="">Select date</option>
<?php
foreach($examyear as $examdate){
echo '<option value= "' .$examdate.'">'.$examdate.'</option>';
}
?>
<span id="error_examdate" class="text-danger"></span>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Exam Number</label>
<input type="text" class="form-control" name="examno2" id="examno" placeholder="Exam Number">
<span id="error_examno" class="text-danger"></span>
</div>
</div>
<br>
<table width="100%" cellpadding="0" cellspacing="0" border="0" class="table table-borderless" id="example">
<tr>
<td width="12%"><label class="control-label">S/NO</label></td>
<td width="53%"><label class="control-label">SUBJECTS</label></td>
<td width="35%"><label class="control-label">GRADE</label></td>
</tr>
<tr>
<td>1</td>
<td>
<?php include("config.php");
$sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
?>
<select name="subject21" class="form-control" id="subject">
<option value="" selected="selected">Select subject</option>
<?php
if($count > 0){
while($row = mysqli_fetch_assoc($query)){
$subject_id = $row['subject_id'];
$subject_name = $row['subject_name'];
echo "<option>" . $row['subject_name'] . "</option>";
}
}else{
echo "<option value = ''>Subject not available</option>";
}
?>
</select>
</td>
<td>
<select name="grade21" class="form-control">
<option value=""> Select</option>
<option value="A1">A1</option>
<option value="B2">B2</option>
<option value="B3">B3</option>
<option value="C4">C4</option>
<option value="C5">C5</option>
<option value="C6">C6</option>
<option value="D7">D7</option>
<option value="E8">E8</option>
<option value="F9">F9</option> </select>
</td>
</tr>
<tr>
<td>2</td>
<td>
<?php include("config.php");
$sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
?>
<select name="subject22" class="form-control" id="subject">
<option value="" selected="selected">Select subject</option>
<?php
if($count > 0){
while($row = mysqli_fetch_assoc($query)){
$subject_id = $row['subject_id'];
$subject_name = $row['subject_name'];
echo "<option>" . $row['subject_name'] . "</option>";
}
}else{
echo "<option value = ''>Subject not available</option>";
}
?>
</select>
</td>
<td>
<select name="grade22" class="form-control">
<option value=""> Select </option>
<option value="A1">A1</option>
<option value="B2">B2</option>
<option value="B3">B3</option>
<option value="C4">C4</option>
<option value="C5">C5</option>
<option value="C6">C6</option>
<option value="D7">D7</option>
<option value="E8">E8</option>
<option value="F9">F9</option> </select>
</td>
</tr>
<tr>
<td>3</td>
<td>
<?php include("config.php");
$sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
?>
<select name="subject23" class="form-control" id="subject">
<option value="" selected="selected">Select subject</option>
<?php
if($count > 0){
while($row = mysqli_fetch_assoc($query)){
$subject_id = $row['subject_id'];
$subject_name = $row['subject_name'];
echo "<option>" . $row['subject_name'] . "</option>";
}
}else{
echo "<option value = ''>Subject not available</option>";
}
?>
</select>
</td>
<td>
<select name="grade23" class="form-control">
<option value=""> Select </option>
<option value="A1">A1</option>
<option value="B2">B2</option>
<option value="B3">B3</option>
<option value="C4">C4</option>
<option value="C5">C5</option>
<option value="C6">C6</option>
<option value="D7">D7</option>
<option value="E8">E8</option>
<option value="F9">F9</option> </select>
</td>
</tr>
<tr>
<td>4</td>
<td>
<?php include("config.php");
$sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
?>
<select name="subject24" class="form-control" id="subject">
<option value="" selected="selected">Select subject</option>
<?php
if($count > 0){
while($row = mysqli_fetch_assoc($query)){
$subject_id = $row['subject_id'];
$subject_name = $row['subject_name'];
echo "<option>" . $row['subject_name'] . "</option>";
}
}else{
echo "<option value = ''>Subject not available</option>";
}
?>
</select>
</td>
<td>
<select name="grade24" class="form-control">
<option value=""> Select </option>
<option value="A1">A1</option>
<option value="B2">B2</option>
<option value="B3">B3</option>
<option value="C4">C4</option>
<option value="C5">C5</option>
<option value="C6">C6</option>
<option value="D7">D7</option>
<option value="E8">E8</option>
<option value="F9">F9</option> </select>
</td>
</tr>
<tr>
<td>5</td>
<td>
<?php include("config.php");
$sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
?>
<select name="subject25" class="form-control" id="subject">
<option value="" selected="selected">Select subject</option>
<?php
if($count > 0){
while($row = mysqli_fetch_assoc($query)){
$subject_id = $row['subject_id'];
$subject_name = $row['subject_name'];
echo "<option>" . $row['subject_name'] . "</option>";
}
}else{
echo "<option value = ''>Subject not available</option>";
}
?>
</select>
</td>
<td>
<select name="grade25" class="form-control">
<option value="">Select </option>
<option value="A1">A1</option>
<option value="B2">B2</option>
<option value="B3">B3</option>
<option value="C4">C4</option>
<option value="C5">C5</option>
<option value="C6">C6</option>
<option value="D7">D7</option>
<option value="E8">E8</option>
<option value="F9">F9</option> </select>
</td>
</tr>
</table>
</div>
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</form>
以上是我的HTML和JavaScript代码。正在使用该JavaScript来根据所选的座位数显示表单。
下面是我的PHP代码
$sitting = mysqli_real_escape_string($conn, $_POST['sitting']);
$schoolname1 = mysqli_real_escape_string($conn, $_POST['schoolname1']);
$examboard1 = mysqli_real_escape_string($conn, $_POST['examboard1']);
$examdate1 = mysqli_real_escape_string($conn, $_POST['examdate1']);
$examno1 = mysqli_real_escape_string($conn, $_POST['examno1']);
$subject11 = mysqli_real_escape_string($conn, $_POST['subject11']);
$grade11 = mysqli_real_escape_string($conn, $_POST['grade11']);
$subject12 = mysqli_real_escape_string($conn, $_POST['subject12']);
$grade12 = mysqli_real_escape_string($conn, $_POST['grade12']);
$subject13 = mysqli_real_escape_string($conn, $_POST['subject13']);
$grade13 = mysqli_real_escape_string($conn, $_POST['grade13']);
$subject14 = mysqli_real_escape_string($conn, $_POST['subject14']);
$grade14 = mysqli_real_escape_string($conn, $_POST['grade14']);
$subject15 = mysqli_real_escape_string($conn, $_POST['subject15']);
$grade15 = mysqli_real_escape_string($conn, $_POST['grade15']);
$schoolname2 = mysqli_real_escape_string($conn, $_POST['schoolname2']);
$examboard2 = mysqli_real_escape_string($conn, $_POST['examboard2']);
$examdate2 = mysqli_real_escape_string($conn, $_POST['examdate2']);
$examno2 = mysqli_real_escape_string($conn, $_POST['examno2']);
$subject21 = mysqli_real_escape_string($conn, $_POST['subject21']);
$grade21 = mysqli_real_escape_string($conn, $_POST['grade21']);
$subject22 = mysqli_real_escape_string($conn, $_POST['subject22']);
$grade22 = mysqli_real_escape_string($conn, $_POST['grade22']);
$subject23 = mysqli_real_escape_string($conn, $_POST['subject23']);
$grade23 = mysqli_real_escape_string($conn, $_POST['grade23']);
$subject24 = mysqli_real_escape_string($conn, $_POST['subject24']);
$grade24 = mysqli_real_escape_string($conn, $_POST['grade24']);
$subject25 = mysqli_real_escape_string($conn, $_POST['subject25']);
$grade25 = mysqli_real_escape_string($conn, $_POST['grade25']);
$enter = "INSERT INTO applicants(sitting, schoolname1, examboard1,
examdate1, examno1, subject11, grade11, subject12, grade12, subject13,
grade13, subject14, grade14, subject15, grade15, schoolname2, examboard2,
examdate2, examno2, subject21, grade21, subject22, grade22, subject23,
grade23, subject24, grade24, subject25, grade25) VALUES('$sitting',
'$schoolname1', '$examboard1', '$examdate1', '$examno1', '$subject11',
'$grade11', '$subject12', '$grade12', '$subject13', '$grade13',
'$subject14', '$grade14', '$subject15', '$grade15','$schoolname2',
'$examboard2', '$examdate2', '$examno2', '$subject21', '$grade21',
'$subject22', '$grade22', '$subject23', '$grade23', '$subject24',
'$grade24', '$subject25', '$grade25')";
$result = mysqli_query($conn, $enter);
if($result){
echo "<script type='text/javascript'>alert('Details Uploaded !!');
</script>";
header('location: success.php');
}else{
echo "<script type='text/javascript'>alert('Details Failed !!');</script>";
}
}
有没有更好的方法可以做到这一点而不重复所有这些主题和等级?
答案 0 :(得分:2)
您正在使用表格名称来泡菜。通过允许您以表格形式指定数组名称,PHP使这一点变得更容易了:
axios.get('http://api.digiart.lt/items/',
{ params:
{ category:"latte" }} )
.then(response => {
let coffee = Object.keys(response.items).map(i => response.items[i])
这会产生此后输出,很容易循环:
<?php
if ($_POST)
{
echo "<pre>";
print_r($_POST);
echo "</pre>";
}
?>
<form method="post">
Grades:
<input name="grade[]" type="text">
<input name="grade[]" type="text">
<input name="grade[]" type="text">
<hr>
<input type="submit" value="Submit">
</form>
这适用于数据列表,但是您的用例有一个附加的维,这使此操作更加困难。你有:
但是,PHP也可以通过指定二阶列表属于哪个子列表来支持它:
Array
(
[grade] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
这会产生这种安排:
<?php
if ($_POST)
{
echo "<pre>";
print_r($_POST);
echo "</pre>";
}
?>
<form method="post">
Board: <input name="board[]" type="text">
Grades:
<input name="grade[0][]" type="text">
<input name="grade[0][]" type="text">
<input name="grade[0][]" type="text">
<hr>
Board: <input name="board[]" type="text">
Grades:
<input name="grade[1][]" type="text">
<input name="grade[1][]" type="text">
<input name="grade[1][]" type="text">
<hr>
<input type="submit" value="Submit">
</form>
您可以在此处看到Array
(
[board] => Array
(
[0] => a
[1] => b
)
[grade] => Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
)
)
中的第0个条目是考试板(一阶列表),board
中的第0个条目本身是一个列表(与第0个板有关的第二阶列表)。
我将把它作为有价值的练习留给您,以将其他元素添加到此处的一阶和二阶列表中。
相对于表格,这是一个足够广泛的话题,可以独自提出另一个问题。需要对此结构进行规范化,因此您可以在单独的表格中保存考试板,考试,学生和成绩。 “等级”可能是中心表格,因为这需要考试和一名学生,而考试又需要董事会。