简体   繁体   English

与mysql两表数据交叉匹配,表中数值显示使用PHP mySql

[英]Data cross matching with mysql two tables, value display in table using PHP mySql

I am having some trouble with php and mysql, I am even not sure how to properly ask the question, it seems very complex.我在使用 php 和 mysql 时遇到了一些问题,我什至不知道如何正确地问这个问题,这似乎很复杂。 Still if anyone can help me, i will be very thankful.不过,如果有人可以帮助我,我将非常感激。

i have two tables (allunit.sql)我有两个表(allunit.sql)

id - unit_name
12       -   MIS
14       -   MIT
15       - ENG

when someone click enroll button from browser (unit_id) will store in enrollment table.当有人从浏览器(unit_id)单击注册按钮时,将存储在注册表中。 if some one enroll into the unit, button will show (Already Enrolled), not not it will show "Enroll" enrollment.sql如果有人注册到该单元,按钮将显示(已注册),而不是它会显示“注册”注册。sql

enroll_id - unit_id
1         - 12
2         - 14

I am using this query我正在使用这个查询

$unit = SELECT * FROM allunit;

$enroll = SELECT * FROM enrollment;

$row_enroll = mysqli_fetch_assoc($enroll);

while($row = mysqli_fetch_assoc($unit)) {
    if($row['id']==$row_enroll['unit_id']){
        $button = 'Already enrolled';
    }else{
        $button = 'Enroll';
    }
?>

<tr>
     <td><?php echo $row['id']; ?></td>
            <td><?php echo $row['unit_name']; ?></td>

           <td><?php echo $button; ?></td>
</tr>
<?php } ?>

if i add one unit button changes to "already Enrolled" for that unit, but if i add more than one, still only one button changes.如果我添加一个单元按钮更改为该单元的“已经注册”,但如果我添加多个,仍然只有一个按钮更改。 other stays same "enroll".其他保持相同的“注册”。

I know my question is reallty messy, hope you will understand.我知道我的问题很混乱,希望你能理解。 Badly need help.急需帮助。 Thank you谢谢

There are two problems I see in your code:我在您的代码中看到了两个问题:

  1. mysqli_fetch_assoc() is called on a MySQL result, not a query. mysqli_fetch_assoc()在 MySQL 结果上调用,而不是查询。 You need to call mysqli_query() first.您需要先调用mysqli_query() You can see an example in the docs: https://www.php.net/manual/en/mysqli-result.fetch-assoc.php您可以在文档中看到一个示例: https://www.php.net/manual/en/mysqli-result.fetch-assoc.php
  2. When you get a result, such as $row_enroll , it's a collection of rows, so you can't use it with a column directly, ie $row_enroll['unit_id'] won't give you anything.当你得到一个结果时,比如$row_enroll ,它是一个行的集合,所以你不能直接将它与列一起使用,即$row_enroll['unit_id']不会给你任何东西。

Finally, it doesn't appear that a comparison between two separate datasets like this is going to work well for you, at least with the current code.最后,看起来像这样的两个独立数据集之间的比较对您来说不会很好,至少在当前代码中是这样。 Consider using JOIN s to return just one dataset.考虑使用JOIN只返回一个数据集。

First, you have to tell the database to run your query, it is not enough to place a query in a text string.首先,您必须告诉数据库运行您的查询,将查询放在文本字符串中是不够的。 This is done, in this case using the query() method.这是完成的,在这种情况下使用query()方法。

Second, as you want to process the Enrolments once for each of the Units, it would be useful to unload at least the Enrolment into an array so it is easily reusable其次,由于您想为每个单元处理一次注册,因此至少将注册卸载到一个数组中会很有用,这样它就可以轻松重用

// assuming you have a connection and its in $con

$sql    = 'SELECT * FROM allunit';
$units   = $con->query($sql);

$sql    = 'SELECT unit_id FROM enrollment';
$res2   = $con->query($sql);
// make an array of just the enrolment id's as that all you need
// so we can use in_array() later to do the test for are you already enrolled
$enrols = [];
while ($row = $res2->fetch_assoc()){
    $enrols[] = $row['unit_id'];
}

while ($unit = $units->fetch_assoc() ) {
    if ( in_array($unit['id'], $enrols) ) {
        $button = 'Already enrolled';
    }else{
        $button = 'Enroll';
    }

?>
<tr>
    <td><?php echo $unit['id']; ?></td>
    <td><?php echo $unit['unit_name']; ?></td>
    <td><?php echo $button; ?></td>
</tr>
<?php 
} // endwhile
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM