Php 自动隐藏已满的选项并将所有数据插入其他表

Php 自动隐藏已满的选项并将所有数据插入其他表,php,mysql,Php,Mysql,我有以下数据库由以下字段组成 **table_choice** field_choiceid(auto increment) (1, 2, 3, 4, 5) field_choicename (C1, C2, C3, C4, C5) field_choicemaximumslot (10, 12, 15, 18, 20) **table_select** field_selectedid(auto increment) field_selectedchoice field_sel

我有以下数据库由以下字段组成

**table_choice**

field_choiceid(auto increment) (1, 2, 3, 4, 5)

field_choicename (C1, C2, C3, C4, C5)

field_choicemaximumslot (10, 12, 15, 18, 20)


**table_select**

field_selectedid(auto increment)

field_selectedchoice

field_selectedby


**table_full_choice**

field_fullid(auto increment)

field_choiceid_full

field_choicename_full

field_choicemaximumslot_full


**table_full_select**

field_selectedid_full(auto increment)

field_selectedchoice_full

field_selectedby_full
我用这段代码自动填充我的下拉列表

<label>CHOICE</label>

<select value="" name="choice" id="choice" required>

<option>SELECT</option>

<?php

$result=mysqli_query($connection,"SELECT * FROM table_choice")or die(mysqli_error,());

while($row=mysqli_fetch_array($result)){

$choiceid=$row['field_choiceid'];
$choicename=$row['field_choicename'];
$choiceslot=$row['field_choiceslot'];
?>

<option value="<?php echo $choicename;?>">

<?php echo $choicename;?>

</option>

<?php } ?>

</select>

如果可以添加已注册的人数和最多人数 谁可以注册数据库,你可以这样做

<form>  
     <select name = "CourseList">

<?php

    $result = mysqli_query($connection,"SELECT * FROM table_choice")or   die(mysqli_error,());

    while($row = mysqli_fetch_array($result)){

        $choiceid = $row['field_choiceid'];
        $choicename = $row['field_choicename'];
        $choiceslot = $row['field_choiceslot'];
        $HowMany = $row['How_Many'];
        $Maximum = $row['Maximum'];

        if(HowMany != Maximum){
            echo "<option value = "$choiceid" selected>$choicename</option>\n";
    }
?>
     </select>
</form>

您可以使用SQL连接进行选择

如果尚未添加外键,则应为字段
table\u choice.field\u choiceid
table\u select.field\u selectedchoice

我希望我没有犯错误,但是这个代码应该可以工作。但首先尝试选择

<form>  
    <select name = "CourseList">
<?php

    $result=mysqli_query($connection,"SELECT tc.*, count(ts.field_selectedchoice) AS c_selected FROM table_choice tc LEFT JOIN table_select ts ON ts.field_selectedchoice = tc.field.choiceid GROUP BY tc.field_choiceid")or die(mysqli_error,());

    while($row=mysqli_fetch_array($result)){

        $choiceid=$row['field_choiceid'];
        $choicename=$row['field_choicename'];
        $choiceslot=$row['field_choiceslot'];
        $maxchoice=$row['field_choicemaximumslot'];
        $selectcount=$row['c_selected'];

        if ($maxchoice > $selectcount){
            echo '<option value='.$choiceid.'>'.$choicename.'</option>';
        }
    ?>
    </select>


如果SQL语句中有错误,我将尝试更正。

糟糕的是,我谦恭地欢迎您使用stackoverflow

根据你的问题,@Spirit说你可以
左加入他们
,然后你可以使用
保留字过滤掉。这样,我就可以在把所有的课程结合在一起之后,过滤掉已经很满的课程(就我在你的问题中所理解的那样)

如需了解更多有关此网站的参考信息。还有更多信息,重点介绍了
具有
所在位置之间的区别:

出于完整性考虑,我在SQLFIDLE中构建了一个数据库示例,示例数据库的构建方式如下:

CREATE TABLE table_choice(
  field_choiceid INT UNSIGNED NOT NULL PRIMARY KEY,
  field_choicename VARCHAR(2) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
  field_choicemaximumslot INT UNSIGNED NULL
);

CREATE TABLE table_select(
  field_selectedid INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  field_selectedchoice INT UNSIGNED NOT NULL,
  FOREIGN KEY fk_select(field_selectedchoice) REFERENCES table_choice(field_choiceid) ON DELETE CASCADE ON UPDATE CASCADE
);

INSERT INTO table_choice VALUES (1,'C1',10),(2,'C2',12),(3,'C3',15),(4,'C4',18),(5,'C5',20);
insert into table_select(field_selectedchoice) VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(4),(5),(3),(2),(2),(2),(5),(4);
CREATE VIEW view_table_choice AS SELECT table_choice.*, count(table_select.field_selectedchoice) 
AS c_selected FROM table_choice LEFT JOIN table_select
ON table_select.field_selectedchoice = table_choice.field_choiceid
GROUP BY table_choice.field_choiceid;
正如您可以看到的查询:

SELECT table_choice.*, count(table_select.field_selectedchoice) 
AS c_selected FROM table_choice LEFT JOIN table_select
ON table_select.field_selectedchoice = table_choice.field_choiceid
GROUP BY table_choice.field_choiceid HAVING c_selected < table_choice.field_choicemaximumslot;
您可以在这段mysql中参考有关视图的内容

因此,使用视图,代码将是:

<label>CHOICE</label>

<select value="" name="choice" id="choice" required>

<option>SELECT</option>

<?php

$result=mysqli_query($connection,"SELECT * FROM view_table_choice WHERE view_table_choice.c_selected < view_table_choice.field_choicemaximumslot")or die(mysqli_error,());

while($row=mysqli_fetch_array($result)){

$choiceid=$row['field_choiceid'];
$choicename=$row['field_choicename'];
$choiceslot=$row['field_choiceslot'];
?>

<option value="<?php echo $choicename;?>">

<?php echo $choicename;?>

</option>

<?php } ?>

</select>
选择
挑选

请告诉我们你们的预期产量好吗?此问题仍不清楚下拉列表中的预期输出是:尚未满的输出在下拉列表中可见,已满的输出在下拉列表中不可见。示例:下拉列表中的原始列表为C1 C2 C3 C4 C5,因为它们尚未满,但C1已满10/10,C3已满15/15,C5已满20/20,则下拉列表必须仅包含C2 C4。您是否有注册人数以及数据库中可以注册的人数?存储在字段_choicename中的每个数据可以设置有多少可以选择该选项(最少10个,最多20个),它存储在字段_choicemaximumslot中。管理员可以输入他/她想要10到20个插槽的数量。在你的问题中,已经有3个答案了。如果其中任何一个可用并解决了您的问题,则可以按向上箭头和勾号。如果我只能添加可以选择特定选项的人数(最大人数),该怎么办。管理员可以将字段_choicemaximumslot设置为10或11或12或13或14或15或16或17或18或19或20,如果设置为10,则10是最大插槽。然后,如果15名注册者中有10名选择了C1(将字段_choicemaximumslot设置为10),C1将在下拉列表中消失/隐藏,这样我就可以避免11/10,下拉列表中所有达到最大插槽的选项都将消失/隐藏。您需要跟踪有多少人能够阻止它。最好的方法是更新数据库,说明有多少人注册了。@Comet但另一方面,@BadSly使用的方法允许在
表_select
被相应修改的情况下记录谁也加入了。我不能在插入外键时通过按钮使用两次插入来使用两次查询。我修改了我的查询,现在它只保存在一个表中。
CREATE VIEW view_table_choice AS SELECT table_choice.*, count(table_select.field_selectedchoice) 
AS c_selected FROM table_choice LEFT JOIN table_select
ON table_select.field_selectedchoice = table_choice.field_choiceid
GROUP BY table_choice.field_choiceid;
<label>CHOICE</label>

<select value="" name="choice" id="choice" required>

<option>SELECT</option>

<?php

$result=mysqli_query($connection,"SELECT * FROM view_table_choice WHERE view_table_choice.c_selected < view_table_choice.field_choicemaximumslot")or die(mysqli_error,());

while($row=mysqli_fetch_array($result)){

$choiceid=$row['field_choiceid'];
$choicename=$row['field_choicename'];
$choiceslot=$row['field_choiceslot'];
?>

<option value="<?php echo $choicename;?>">

<?php echo $choicename;?>

</option>

<?php } ?>

</select>