I created PHP code to search with form and display with table from phpMyAdmin's data. I want to make form and table in PHP. My search form is working but display table in PHP shows error. How can I fix it?
<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>
Here is a code where you can search for customer name by typing on the input field. When click on search button the code runs with isset() function and the table with data is shown.
Comment on this if you need any other help
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>