php - 让用户从数据库中选择某人 PHP

标签 php html mysql

假设我们根据用户标准执行查询。完成此查询后,我向用户展示了一个数组,其中包含符合他的条件的所有人员。

像这样:

请注意,我在每个人的右侧添加了一个按钮,由于此数组是动态的,因此我正在努力弄清楚如何知道选择了哪个人。

我发布了这个代码(但是,它很长......)

$sql1 = "SELECT DISTINCT pid FROM open_position, person
                 WHERE open_position.cid=$s_cid AND (person.prof=open_position.field 
                    OR person.studies LIKE '%open_position.studies%'
                    OR person.skillz LIKE '%open_position.skillz%' 
                    OR person.languages LIKE '%open_position.pref_languages%')";

        if ($result1 = mysqli_query($dbconn, $sql1)){

            $data = array();
            while ($row = mysqli_fetch_assoc($result1)) {
                $data[] = $row["pid"];
            }

            mysqli_free_result($result1);

            ?>
            <table style="float:center" class="table_center">
                <tr>
                    <th id="top_left">pid</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Address</th>
                    <th>Studies</th>
                    <th>Skills</th>
                    <th>Languages</th>
                    <th>Hired</th>
                    <th id="top_right">Available From</th>
                </tr>

            <?php
            $size = count($data);
            $i = 0;
            while ($i < $size) {
                $sql2 = "SELECT * FROM person WHERE pid = '$data[$i]'";
                if ($result2 = mysqli_query($dbconn, $sql2)) {
                    while ($row2 = mysqli_fetch_array($result2)) {
                        ?>
                        <tr>
                            <td>
                                <?php 
                                    echo $row2['pid'];
                                ?>
                            </td>
                            <td>
                                <?php 
                                    echo $row2['first_name'];
                                ?>
                            </td>
                            <td>
                                <?php
                                    echo $row2['last_name'];
                                ?>
                            </td>
                            <td>
                                <?php
                                    echo $row2['address'];
                                ?>
                            </td>
                            <td>
                                <?php
                                    echo $row2['studies'];
                                ?>
                            </td>
                            <td>
                                <?php
                                    echo $row2['skillz'];
                                ?>
                            </td>
                            <td>
                                <?php
                                    echo $row2['languages'];
                                ?>
                            </td>
                            <td>
                                <?php
                                    if ($row2['hired'] == "1"){
                                        echo "YES";
                                    }
                                    else {
                                        echo "NO";
                                    }
                                ?>
                            </td>
                            <td>
                                <?php
                                    echo $row2['availability'];
                                ?>
                            </td>
                            <td>
                                <input type="submit" class="button" name="hire" value="HIRE
                                <?php 
                                    $_SESSION['hired_pid'] = $data[$i];

                                ?>"></input>
                            </td>
                        <tr>
                        <?php
                    }
                }

                $i = $i + 1;
            }
            ?></table>
            <?php
        }

有什么想法吗???

最佳答案

好的实现这个。对于按钮本身,您可以添加它以重定向到其中包含人员 ID 的 url。类似这样。

<a href='hire.php?pid=<?=$row2['pid'];?>'><input type='button' value='hire' /></a>

在 hire.php 中获取人的 id,然后做任何你想做的事。这可以根据您的要求以多种方式完成。这只是一个示例想法。

关于php - 让用户从数据库中选择某人 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34935264/

相关文章:

php - 我如何在我的应用程序中使用 json?

php - 更改选择选项值时网页上偶尔显示白色空白

javascript - 关闭 W3School Accordion 的所有按钮(仅限 html、css、js)

php - 使用 PHP 和 MySQL 项目构建登录页面

mysql - 无法索引表 - MySQL 服务器已经消失

php更改数据库日期值

javascript - 产品页面上的数量发生变化时自动更新价格 Magento

php - 当按 id 或代码搜索时,如何从 codeigniter 中的 mysql 结果获取行索引

javascript - jQuery 添加和删除带有嵌套 div 的 HTML 元素

html - css 没有加载到 rails 上