I've followed this tutorial thoroughly, and had success with it. However, it leads me to a dead end where I have to pay to access the tutorial on implementing a "Search" functionality to it.
I was wondering if anyone could help me out on how I can implement a search functionality given how the tutorial is constructed? Tutorials seems like they don't work with this.
I've tried this on a separate page so far, but it doesn't seem to work - also I don't know how it can be implemented on my actual site.
<?php
mysql_connect("localhost","root","") or die("could not connect");
mysql_select_db("kkp") or die("could not find db!");
$output ='';
//collect
if (isset($_POST['search'])){
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysql_query("SELECT * FROM personal_info WHERE FirstName LIKE '%$searchq%' OR SurName LIKE '%$searchq%'") or die("could not search");
$count = mysql_num_rows($query);
if($count == 0){
$output = 'There was no search results!';
}else{
while($row = mysql_fetch_array($query)){
$fname = $row['FirstName'];
$lname = $row['SurName'];
$id = $row['id'];
$output .= '<div>'.$fname.''.$lname.'</div>';
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<form method="post" action="search.php"></form>
<input type="text" name="search" placeholder="Search for student">
<input type="submit" value="Submit">
</body>
</html>
<?php print("$output");?>
Also, here's where I'm trying to implement a search functionality. It just displays a table with the entries I've added to the database.
<div class="row">
<h3>List of Student Volunteers</h3>
</div>
<div class="row">
<table class="table table-striped table-bordered">
<thead>
<tr>
<td>Name</td>
<td>Email Address</td>
<td>Mobile Number</td>
<td>Action</th>
</tr>
</thead>
<tbody>
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM personal_info ORDER BY id DESC';
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['FirstName'] . '</td>';
echo '<td>'. $row['EmailAddress'] . '</td>';
echo '<td>'. $row['MobileNum'] . '</td>';
echo '<td width=250>';
echo '<a class="btn" href="read.php?id='.$row['id'].'">Read</a>';
echo ' ';
echo '<a class="btn" href="update.php?id='.$row['id'].'">Update</a>';
echo ' ';
echo '<a class="btn" href="delete.php?id='.$row['id'].'">Delete</a>';
echo '</td>';
echo '</tr>';
}
Database::disconnect();
?>
mysql_connect ...- find a newer tutorial, one that's not using deprecated/obsolete functions and ridding the code with SQL injection vulnerabilities. You probably want to start with pdo : php.net/manual/en/book.pdo.php and, for search functionality in MySQL, FULLTEXT indexes : dev.mysql.com/doc/refman/5.7/en/fulltext-natural-language.htmlMATCH ... AGAINSTwith MySQL - the search functionality will be way, way quicker than anything you try withLIKE '% ... %'.... though you may hit the odd issue with stopwords