Creé código PHP para buscar con formulario y mostrar con tabla de datos de phpMyAdmin. Quiero hacer un formulario y una tabla en PHP. Mi formulario de búsqueda funciona pero la tabla de visualización en PHP muestra un error. ¿Cómo puedo arreglarlo?
<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="eg) 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>Aquí hay un código donde puede buscar el nombre del cliente escribiendo en el campo de entrada. Al hacer clic en el botón de búsqueda, el code se ejecuta con la función isset() y se muestra la tabla con los datos.
Comenta sobre esto si necesitas otra ayuda.
CÓDIGO PHP/HTML
<?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>