Comment faire un bare de recherche en php PDO ?

Asked  Jan 03, 09:47 am 36 Views Add Your Answer
Asked  Jan 03, 09:47 am
Php
0 Dislikes
0 Comments

1 Answers

Edited  Jan 03, 10:29 pm

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
0 Comments
×
SignUp with Email
X