manao
manao

Reputation: 5

how to use substr function in search

i need to use substr to get the name who have written letters or if i write a part of the name

 if(isset($_GET['search'])){
 $sql="SELECT * FROM students WHERE first_name='".$_POST['sname']."' OR         last_name='".$_POST['fname']."'";
 $record=mysql_query($sql);}

<?php
echo"<form action='admin.php?page=3&search' method='Post'>";
echo"<input type='text' name='sname' value='ex: Student Name'> &nbsp; &nbsp;";
echo"<input type='text' name='fname' value='ex: Family Name'> &nbsp; &nbsp;";
echo"<button type='submit' name='search'   class='btn' >Search</button>";
echo"</form>";
?>

Upvotes: 0

Views: 90

Answers (2)

Aju John
Aju John

Reputation: 2234

I think you need to change the query, Use LIKE in the query:

 $sql="SELECT * FROM students WHERE first_name like '%".$_POST['sname']."%' OR last_name like '%".$_POST['fname']."%' ";

Upvotes: 1

RJParikh
RJParikh

Reputation: 4166

Use LIKE %.

if(isset($_GET['search']))
{
 $sql="SELECT * FROM students WHERE first_name LIKE '%".$_POST['sname']."%' OR last_name LIKE '%".$_POST['fname']."%'";
 $record=mysql_query($sql);
}

Upvotes: 1

Related Questions