Hi.. i create a search button in my program..to search in a particular record in my database..what are the codes that i'll be using in order to search a record? tnx in advance..Godbless

Recommended Answers

All 19 Replies

Member Avatar for GreenDay2001

You have to creater an SQL query. Search for SQL tutorial, if you dunno what it is, or how to work with it. After creating an SQL Query, use PHP to connect to your database, execute command and then display result.

You have to creater an SQL query. Search for SQL tutorial, if you dunno what it is, or how to work with it. After creating an SQL Query, use PHP to connect to your database, execute command and then display result.

This the code i used in searchin' some records..but it didn't worked. The error was

Forbidden

You don't have permission to access /41e1/myExample/search/< on this server.

here's my code..

<?php
/*set varibles from form */
$search = $_POST['search'];
Trim ($search);
/*check if search term was entered*/
If (!$search){
    echo 'Please enter a search term.';
}
/*add slashes to search term*/
If (!get_magic_quotes_gpc())
{
$search = addslashes($search);
}

/* connects to database */
$connection = mysql_connect('localhost', 'root') 
        or die ("Could not connect to server ... \n" . mysql_error ());
    mysql_select_db($db) 
        or die ("Could not connect to database ... \n" . mysql_error ());

Exit;
}
/*query the database*/
$query = "select * from students where name like '%$varchar%'";
$result = $dbconn->query($query);
/*number of rows found*/
$num_results = $result->num_rows;

echo '<p>Found: '.$num_results.'</p>';
/*loops through results*/
For ($I=0; $I <$num_results; $I++)
{
$num_found = $I + 1;
$row = $result->fetch_assoc();
Echo "$num_found. ".($row['name'])." <br />";
Echo "<table border='1'>
<tr>
<th>name</th>
<th>studno</th>
<th>course</th>
<th>department</th>
</tr>";

  echo "<tr>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['studno'] . "</td>";
  echo "<td>" . $row['course'] . "</td>";
  echo "<td>" . $row['department'] . "</td>";
Echo "</tr>";
}
Echo "</table>";

?>
Member Avatar for GreenDay2001

Open your FTP client and click on properties of that file, and set permissions to 0777, that is tick all the checkboxes and see if it works!

Open your FTP client and click on properties of that file, and set permissions to 0777, that is tick all the checkboxes and see if it works!

How can i find the FTP client? im using WAMP as my server.

Member Avatar for GreenDay2001

Oh windows, um I dont have much idea about changing permissions in it, but in XP Sp3, Vista, 7 right click and select properties. Select security tab, and select Full Control for users below.

make sure you are pointing the <form action="FILE_NAME.PHP" to your php file!

make sure you are pointing the <form action="FILE_NAME.PHP" to your php file!

yeah..but still the problem is there..I know the problem lies on my code above..Could you pls recode my coding..I dont what's the problem inside..hmm..

I could try if I have all Files & Sql Database structure (and some sample data for testing)!

Zip them and PM them to me!

I could try if I have all Files & Sql Database structure (and some sample data for testing)!

Zip them and PM them to me!

ok tnx...reply as soon as you have the idea regarding with my codes..

Search.php page

<form action="show.php" method="post">

<input type="text" value="Enter Site ID" name="id" maxlength="30" onfocus="this.value=''">
</td>
</tr>
<tr>
            <td align="center" value="Search"/>
</form>

show.php page

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$a= $_POST["id"];
mysql_select_db("onm", $con);

$result = mysql_query("SELECT * FROM table_name
WHERE Id REGEXP '$a'");

while($row = mysql_fetch_array($result))
  {
  echo "<table  align=center>";
  echo "<tr>";
  echo "<th bgcolor=#5D9BCC >SiteID</th>";
  echo "<td bgcolor=#FEE9A9>" . $row['SiteId'] . "</td>";
  echo "</tr>";

  }
echo "</table>";


mysql_close($con);

?>

Search.php page

<form action="show.php" method="post">

<input type="text" value="Enter Site ID" name="id" maxlength="30" onfocus="this.value=''">
</td>
</tr>
<tr>
            <td align="center" value="Search"/>
</form>

show.php page

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$a= $_POST["id"];
mysql_select_db("onm", $con);

$result = mysql_query("SELECT * FROM table_name
WHERE Id REGEXP '$a'");

while($row = mysql_fetch_array($result))
  {
  echo "<table  align=center>";
  echo "<tr>";
  echo "<th bgcolor=#5D9BCC >SiteID</th>";
  echo "<td bgcolor=#FEE9A9>" . $row['SiteId'] . "</td>";
  echo "</tr>";

  }
echo "</table>";


mysql_close($con);

?>

ok..i'll try to run this code..Hope it will work..tnx..

ok tnx...reply as soon as you have the idea regarding with my codes..

I have trouble regarding with my codes here in searching file..
here's the code:It works but the output are all the records in my database..i only enter name..and it must appear only the name..but all the records displayed on my webpage..

<?php
	mysql_connect ("localhost", "root")  or die (mysql_error());
	mysql_select_db ("records");

	$term = $_POST['term'];
	 
	$sql = mysql_query("select * from students where name like '%$string%'");
	 
	while ($row = mysql_fetch_array($sql)){

	    echo '<br/> name: '.$row['name'];
	    echo '<br/> course: '.$row['course'];
	   
	    echo '<br/><br/>';
	    }
	 
	?>

remove:
echo '<br/> course: '.$row;

ok..i'll try to run this code..Hope it will work..tnx..

hey,ayesha,hmmm your code did not worked at all..it seems that there's an error inside but i dont what it is..can you find it?i want my searching be more interesting.. first, i want to select in searching if student no,course or by name then if i choose name, i'll enter the name of the student in a textbox then click the search button. And it will display all information of that particular student..that's what i want in my page in searching..hmm Godbless...i'll wait for your reply here... :)

Search.php page

<form action="show.php" method="post">

<input type="text" value="Enter Site ID" name="id" maxlength="30" onfocus="this.value=''">
</td>
</tr>
<tr>
            <td align="center" value="Search"/>
</form>

show.php page

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$a= $_POST["id"];
mysql_select_db("onm", $con);

$result = mysql_query("SELECT * FROM table_name
WHERE Id REGEXP '$a'");

while($row = mysql_fetch_array($result))
  {
  echo "<table  align=center>";
  echo "<tr>";
  echo "<th bgcolor=#5D9BCC >SiteID</th>";
  echo "<td bgcolor=#FEE9A9>" . $row['SiteId'] . "</td>";
  echo "</tr>";

  }
echo "</table>";


mysql_close($con);

?>

hey,ayesha,hmmm your code did not worked at all..it seems that there's an error inside but i dont what it is..can you find it?i want my searching be more interesting.. first, i want to select in searching if student no,course or by name then if i choose name, i'll enter the name of the student in a textbox then click the search button. And it will display all information of that particular student..that's what i want in my page in searching..hmm Godbless...i'll wait for your reply here... :)

remove:
echo '<br/> course: '.$row;

smartness,i already removed echo '<br/> course: '.$row; but still the same error..all records are displayed.

If you know the students Exact name, then this code:

$sql = mysql_query("select * from students where name like '%$string%'");

should be:

$sql = mysql_query("select * from students where name = '$string'");

By the way, you don't have any $string defined, you have $term, so in Fact your query should be:

$sql = mysql_query("select * from students where name = '$string'");

p.s. Im talking about the code in this post

If you know the students Exact name, then this code:

$sql = mysql_query("select * from students where name like '%$string%'");

should be:

$sql = mysql_query("select * from students where name = '$string'");

By the way, you don't have any $string defined, you have $term, so in Fact your query should be:

$sql = mysql_query("select * from students where name = '$string'");

p.s. Im talking about the code in this post

What's the problem with my code below:

<?php
   
      mysql_connect ("localhost", "root") or die (mysql_error());
   
      mysql_select_db ("records");
   

   
      $term = $_POST['term'];

       

      $sql = mysql_query("select * from students where name like '%$term%' OR studno like '$term%'");

        echo "<table width='600' cellspacing='0' cellpadding='0' border='1' align='center'>";
         echo "<tr><th bgcolor='#99FFCC'>Student No:</th><th bgcolor='#99FFCC'>Name</th> <th bgcolor='#99FFCC'>Course</th> <th bgcolor='#99FFCC'>Year</th><th bgcolor='#99FFCC'>Department</th></tr>";

      while ($row = mysql_fetch_assoc($sql)){

       echo '<td><center>' . $row['studno'] . '</td>';
       echo '<td><center>' . $row['name'] . '</td>';
	echo '<td><center>' . $row['course'] . '</td>';
	echo '<td><center>' . $row['year'] . '</td>';
	echo '<td><center>' . $row['department'] . '</td>';
        echo "</tr>";

            }
        echo "</table>";


      ?>

i used this code in searching a particular term or record in my database..it works well.this function search very well..but the problem is when i do not enter any term in the textbox, i just click the search button without entering any term from the textbox,they display all the records in my database..it must display,"Pls enter a name to search".. or maybe bacause i used this:

$sql = mysql_query("select * from students

and i didn't used if else statement for this..is there somebody would like to re-encode my codes using if-else statement.?..tnx in advance..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.