gpt4 book ai didi

php - 如何在php中以表的形式显示同一数据库中选定表中的数据?

转载 作者:行者123 更新时间:2023-11-30 21:25:43 25 4
gpt4 key购买 nike

我在一个网站上工作,我正在从用户那里输入学期号,我想显示他们相应输入的学期中的科目。为此,我计划为每个学期创建包含主题的表格。现在,我成功地从一个表中检索数据。但是我需要按照学生输入的学期获得科目。以下是代码,非常适用于第 5 学期。请根据上述要求提出修改建议。

<!DOCTYPE html>
<html>
<head>
<title></title>
<!-- very important -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="subject.css">
</head>
<body>

<h1 class="text-center text-capitalize font-weight-bold">Subject Information</h1>

<div class="container">
<div>
<br>

<div>
<form class="form-inline">
<!-- <div class="mx-auto" style="width: 200px;"> -->
<div class="form-group">
<label for="Sem"> Semester: </label>
<div>
<input type="text" name="sem_no" placeholder="Enter current semester" id=sem class="form-control">
</div>
<br>

</div>
</form>
</div>
</div>
</div>
<div>

<!-- TABLE DISPLAYED WITH DATA FROM RESPECTIVE DATABASE -->
<div class= "container">
<div class="col-lg-12 ">
<br><br>
<h2 class="text-center">Subjects</h2>

<table class="table table-striped table-hover table-bordered">
<tr>
<th>Subject code</th>
<th>Subject Name</th>
<th>Faculty name</th>
</tr>

<?php
$con = mysqli_connect('localhost','root');

mysqli_select_db($con,'subjects');

$q= "select * from sem5";

$query = mysqli_query($con,$q);

while($res = mysqli_fetch_array($query))
{
?>

<tr>
<td><?php echo $res['subject_no']; ?></td>
<td><?php echo $res['subject_name']; ?></td>
<td><?php echo $res['faculty_name']; ?></td>
</tr>

<?php
}
?>
</table>

</div>
</div>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</body>
</html>

请提出修改建议。将非常有义务。

最佳答案

您需要执行 HTTP GET 请求并使用 PHP 获取值。然后使用 PHP 进行 SQL 查询。这是最简单的形式。

我添加了一个提交按钮,用于提交表单并使用附加到 URL 的参数执行 HTTP GET。提交后你会看到?sem_no=x。那是 Query String .

...
...
<div class="form-group">
<label for="Sem"> Semester: </label>
<div>
<input type="text" name="sem_no" placeholder="Enter current semester" id=sem class="form-control">
</div>
<div>
<input type="submit"/>
</div>
<br>
...
...
...
...
//check the value was submitted and it is not blank
<?php if(isset($_GET['sem_no']) && !empty($_GET['sem_no'])) { ?>

<!-- TABLE DISPLAYED WITH DATA FROM RESPECTIVE DATABASE -->
<div class= "container">
<div class="col-lg-12 ">
<br><br>
<h2 class="text-center">Subjects</h2>

<table class="table table-striped table-hover table-bordered">
<tr>
<th>Subject code</th>
<th>Subject Name</th>
<th>Faculty name</th>
</tr>

<?php
$con = mysqli_connect('localhost','root');

mysqli_select_db($con,'subjects');

$q= 'select * from sem'.$_GET['sem_no'];

$query = mysqli_query($con,$q);

while($res = mysqli_fetch_array($query))
{
?>

<tr>
<td><?php echo $res['subject_no']; ?></td>
<td><?php echo $res['subject_name']; ?></td>
<td><?php echo $res['faculty_name']; ?></td>
</tr>

<?php
}
?>
</table>

</div>
</div>
<?php
//closing if
} ?>
...
...

如前评论所述,此数据库设计很差。我知道你正在学习。您是否考虑过使用包含 Semester 列的 subjects 表?然后你可以做这样的查询:

SELECT * FROM subjects where semester_number = x

除此之外还有更好的方法,但这对您来说是一个很好的起点。

关于php - 如何在php中以表的形式显示同一数据库中选定表中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59176501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com