douejuan9162 2018-03-14 13:57
浏览 49

PHP网站搜索

I'm having trouble getting my php search project working properly, having followed a guide, I don't fully understand the guide/code. My search bar will allow me to search for jobs in the database, but currently it shows all jobs and filters the one you search.

Is it possible to display these jobs as links, where it will take you to another page and display the currently selected job.

Here is my current code:

<?php
    require 'config.php';
    if(isset($_POST['search']))
    {
        $valueToSearch = $_POST['valueToSearch'];
        // search in all table columns
        // using concat mysql function
        $query = "SELECT * FROM `job` WHERE CONCAT(`location`, `description`, `budget`, `duedate`,`title`) LIKE '%".$valueToSearch."%'";
        $search_result = filterTable($query);

    }
     else {
        $query = "SELECT * FROM `job`";
        $search_result = filterTable($query);
    }

    // function to connect and execute the query
    function filterTable($query)
    {
        $conn = mysqli_connect("localhost", "root", "", "bid4myjob");
        $filter_Result = mysqli_query($conn, $query);
        return $filter_Result;
    }

    ?>

    <!DOCTYPE html>
    <html>
        <head>
            <title>PHP HTML TABLE DATA SEARCH</title>
            <style>
                table,tr,th,td
                {
                    border: 1px solid black;
                }
            </style>
        </head>
        <body>

            <form action="php_html_table_data_filter.php" method="post">
                <input type="text" name="valueToSearch" placeholder="Value To Search"><br><br>
                <input type="submit" name="submit" value="Search"><br><br>

                 <table>
                    <tr>
                        <th>Title</th>
                        <th>Location</th>
                        <th>Description</th>
                        <th>Budget</th>
                        <th>Due date</th>
                    </tr>

          <!-- populate table from mysql database -->
                    <?php while($row = mysqli_fetch_array($search_result)):?>
                    <tr>
                        <td><?php echo $row['title'];?></td>
                        <td><?php echo $row['location'];?></td>
                        <td><?php echo $row['description'];?></td>
                        <td><?php echo $row['budget'];?></td>
                        <td><?php echo $row['duedate'];?></td>
                    </tr>
                    <?php endwhile;?>
                </table>
            </form>

        </body>
    </html>
  • 写回答

1条回答 默认 最新

  • dstd2129 2018-03-14 14:38
    关注

    Your problem is this line:

    if(isset($_POST['search']))
    

    There's no variable called "search" which will be submitted by your form, so its value will never be set, and this if block will never be entered. I suspect you've confused the "name" attribute which determines the variable's name in the POST array, with its value ("Search", in the case of your button). Try

    if(isset($_POST['submit']))
    

    instead.

    See also my comments above about your security problems and aim to fix those a.s.a.p.

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程