Comment faire un bare de recherche en php PDO ?
1 Answers
Search bar using Php PDO:-
Create connect conn.php
<?php
$conn = new PDO( 'mysql:host=localhost;dbname=db_pdo_search', 'root', '');
if(!$conn){
die("Error: Failed to coonect to database!");
}
?>
<?php
require 'conn.php';
if(ISSET($_POST['search'])){
?>
<table class="table table-bordered">
<thead class="alert-info">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
$keyword = $_POST['keyword'];
$query = $conn->prepare("SELECT * FROM `member` WHERE `firstname` LIKE '%$keyword%' or `lastname` LIKE '%$keyword%' or `address` LIKE '%$keyword%'");
$query->execute();
while($row = $query->fetch()){
?>
<tr>
<td><?php echo $row['firstname']?></td>
<td><?php echo $row['lastname']?></td>
<td><?php echo $row['address']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}else{
?>
<table class="table table-bordered">
<thead class="alert-info">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
$query = $conn->prepare("SELECT * FROM `member`");
$query->execute();
while($row = $query->fetch()){
?>
<tr>
<td><?php echo $row['firstname']?></td>
<td><?php echo $row['lastname']?></td>
<td><?php echo $row['address']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
?>
0
Likes
0
Dislikes
Likes
Dislikes
0
Comments
Login to add comment