如何在php中创建搜索表单和显示表?

时间:2017-04-29 23:56:08

标签: php html mysql

我创建了PHP代码以使用表单进行搜索,并使用phpMyAdmin数据中的表格进行显示。 我想用PHP制作表格和表格。我的搜索表单正在运行,但PHP中的显示表显示错误。 我该如何解决?

        <div class="container-fluid">
            <form class="form col-md-8" id="form_Show" role="form" form action="Show.php" method="post">
                <legend>Show Customers table</legend>
                <h5>Put any characters in Customer's name to search the data</h5>
                <div class="form-group">
                    <label for="Cstm_name" class="col-sm-2">Customer Name</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control" name="show" id="show" placeholder="e.g) A, L or J">
                    </div>
                    <div class="row"></div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" class="btn btn-primary">Save</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>

    <?php
    $host = "localhost";
    $user = "";
    $password = "";
    $database = "";

    mysql_connect($host,$user,$password) or die("mysql_connect error");
    mysql_select_db($database) or die("mysql_select_db error");
    if(isset($_POST['show'])){
        $search = $_POST['show'];
        $search = preg_replace("#[^0-9a-z]#i","",$search);
        $sql = mysql_query("SELECT * FROM Customers") or die("could not search");
        ?>
        <table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
            <thead>
                <tr>
                    <th>id</th>
                    <th>Name</th>
                    <th>Adress</th>
                    <td>Cell phone</td>
                </tr>
            </thead>
            <tbody>
                <?php while( $row = mysql_fetch_array( $sql ) ){
                    echo
                    "<tr>
                    <td>{$row\['Cstm_id'\]}</td>
                    <td>{$row\['Cstm_name'\]}</td>
                    <td>{$row\['Cstm_addrs'\]}</td>
                    <td>{$row\['CCell_no'\]}</td>
                    </tr>\n";
                }
                ?>
            </tbody>
        </table>

1 个答案:

答案 0 :(得分:0)

这是一个代码,您可以通过在输入字段中键入来搜索客户名称。单击搜索按钮时codeisset()函数运行,并显示包含数据的表。

如果您需要任何其他帮助,请对此进行评论

SQL TABLE IMAGE SQL TABLE IMAGE HERE

PHP / HTML CODE

<?php 
        $link = new mysqli('localhost','root','admin','demo1');
        if($link->connect_error){
            die("Connection Failed".$link->connect_error);
        }
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title></title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </head>
    <body>

    <div class="container">
        <br><br>
        <div class="col-md-5">
      <form action="" method="get">
        <div class="form-group">
            <input type="text" name="search" class="form-control" placeholder="Search Customer Name Here..."/>
        </div>
        <div class="form-group">
            <input type="submit" name="search_btn" class="btn btn-default" value="Search"/>
        </div>
      </form>

      <?php
        if(isset($_GET['search_btn'])){

            $search_var = $_GET['search'];

            $sql = "SELECT * FROM search_form WHERE customer_name LIKE '%".$search_var."%'";
            if($res = $link->query($sql)){

        ?>
            <table class="table table-striped">
                <thead>
                  <tr>
                    <th>Custmer id</th>
                    <th>Customer Name</th>
                  </tr>
                </thead>
                <tbody>
        <?php
            if($res->num_rows > 0){

                while($row = $res->fetch_assoc()){
        ?>
                    <tr>
                        <td><?php echo $row['id'];?></td>
                        <td><?php echo $row['customer_name'];?></td>
                    </tr>
        <?php   
                }   
            }
            else
            {
        ?>
                <tr>
                    <td colspan="2">Not Found<?php echo $link->error;?></td>
                </tr>   
        <?php       
            }
        ?>
                </tbody>
            </table>
        <?php
            }
            else
            {
                echo "Failed".$sql;
            }
        }
      ?>
      </div>
    </div>

    </body>
    </html>