php - 如何从多个 SQL 表中获取值并在 html/php 表中显示?

标签 php html mysql forms html-table

我有一个名为 result 的 SQL 数据库,其中包含表 ressub。 例如, res 表的列和内容是:

sno regno        name       sub1  sub2  sub3  sub4  sub5  

1   1DU12CS100   student1   70    80    85    70    90     
2   1DU12CS101   student2   75    70    90    80    70
3   1DU12EE015   student3   80    85    70    50    65
4   1DU14CS123   student4   88    85    85    90    70
5   1DU13ME050   student5   85    90    70    60    55

sub 表的列和内容是:

Sno   batname     sub1      sub2       sub3       sub4       sub5

1     1DU12CS     Maths     English    Hindi      Urdu       Social 
2     1DU12ME     Sanksrit  Chinese    Japanese   French     Dutch
3     1DU12EE     Circuit   Electrical Electronic Maths      Hindi
4     1DU14CS     Hindi     Maths      Urdu       Science    Maths
5     1DU13ME     Computer  Maths      Electrical Mechanical GK 

我想从表 res 和表 sub 中获取一些值并显示在 php/html 表中。

1DU12CS100  -- 
                                   1DU ->college code
                                   12 ->Student admission year
                                   CS ->computer science
                                   100->roll no of student

当有人在php表单中输入1DU12CS100时,结果应该是这样显示的...

Subjects    Marks

Maths       70
English     80
Hindi       85
Urdu        70
Social      90

而当有人输入1DU13ME050时,那么显示应该是

Subjects      Marks

Computer      85
Maths         90
Electrical    70
Mechanical    60
GK            55

php表单代码为

<!DOCTYPE HTML>
<html> 
<body>

<form action="result.php" method="post">
Enter your Reg No: <input type="text" name="regno"><br>
<input type="submit">
</form>

</body>
</html>

result.php 代码是//这段 php 代码应该做哪些修改??

<?php
$servername = "localhost";
$username = "myresult";
$password = "abcdefg";
$dbname = "myresult";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$regno = mysqli_real_escape_string($conn, $_POST['regno']);

$sql = "SELECT * FROM myresult WHERE regno LIKE '$regno'"; // What changes should be here ??
$result = $conn->query($sql);
$columns = array();
$resultset = array();
while ($row = mysql_fetch_assoc($result)) {
    if (empty($columns)) {
        $columns = array_keys($row);
    }
    $resultset[] = $row;
}


if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "{$row['sub1']}{$row['sub2']}"; // What changes should be made here??



// Print the data
while($row = mysql_fetch_row($result)) {
    foreach($row as $_column) {
        echo "{$_column}";
    }
}
    }
} else {
    echo "Result Not Found";
}
$conn->close();

?>

最佳答案

$sql = "SELECT sub.batname, sub.sub1 as subsub1, sub.sub2 as subsub2, ...,
        Res.sub1 as ressub1, ...
    FROM res 
    JOIN sub ON sub.batname = substr(res.regno, 1, 7)
    WHERE regno LIKE '$regno'";

(我没有列出所有的列。由于您将列命名为相同的名称,因此您必须指定别名。更改名称是个好主意 - subn 不是一个好名字。)

然后往下...

while ($row = $result->fetch_assoc()) { 
    echo "{$row['ressub1']}{$row['subsub1']}";

关于php - 如何从多个 SQL 表中获取值并在 html/php 表中显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29187218/

相关文章:

PHP 强制下载带空格的文件名

javascript - 将 .txt 文件加载到 textarea Javascript?

javascript - 使 div 'fullscreen' onClick 一个按钮?

mysql - 如何运行包含 load data infile 语句的 sql 文件

php - Typoscript 和访问 $this->settings 的 TYPO3 问题

javascript - 在 .js 文件和性能中使用 php

php - 如何在表单提交时创建cookie?

javascript - 具有特定父输入的加号减号按钮不起作用

mysql - 增加WHERE子句的条件会提高搜索速度吗?

MYSQL查询根据查询结果从同一个表中的2条不同记录中提取数据