I'm new to php mysql. i'm creating website for my community. help me out...
My web page has one search page. user to feed few letters in the search box. it is proceesed and the result displaced in a table.
here is my code
for getting input : search.php
<html>
<body>
<form name="form" action="searchproc.php" method="get">
<input type="text" class="textfield" name="name" />
<input type="submit" name="Submit" value="Search" />
</form>
</body>
</html>
for processing the searched query : searchproc.php
<html>
<body>
<?php
// Inialize session
session_start();
// Check, if user is already login, then jump to secured page
if (!isset($_SESSION['empno'])) {
header('Location: login.php');
}
?>
<?php
include 'config.php';
include 'opendb.php';
// Get the search variable from URL
$var = @$_GET['name'] ;
$trimmed = trim($var);
//trim whitespace from the stored variable
// rows to return
$limit=10;
// check for an empty string and display a message.
if ($trimmed == "")
{
echo "Please enter a search...";
exit;
}
// check for a search parameter
if (!isset($var))
{
echo "We dont seem to have a search parameter!";
exit;
}
// Build SQL Query
$query = "select * from detail where name like \"%$trimmed%\"
order by empno"; // EDIT HERE and specify your table and field names for the SQL query
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// If we have no results, offer a google search as an alternative
if ($numrows == 0)
{
echo "Results</br></br>";
echo "Sorry, your search: "" . $trimmed . "" returned zero results";
exit();
}
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
// display what the person searched for
echo "You searched for: "" . $var . """;
// begin to show results set
$count = 1 + $s ;
// now you can display the results returned
echo "<table border='1'>
<tr>
<th>Serial No</th>
<th>Emp No</th>
<th>Name</th>
<th>Branch</th>
</tr>";
while ($row= mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $count. "</td>";
echo "<td>" . $row['empno'] . "</td>";
echo '<td> <a href="profile.php?uid='.$row['empno'].'">'.$row['name'].'</a> </td>';
echo "<td>" . $row['branch'] . "</td>";
echo "</tr>";
$count++ ;
}
$currPage = (($s/$limit) + 1);
//break before paging
echo "<br />";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
Prev 10</a>  ";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+$limit;
echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
}
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $numrows</p>";
echo "<br />";
include 'closedb.php';
?>
</body>
</html>
Now, the result is displayed in the table perfectly, with a hyperlink at each name. However, when i click the link profile of the employee is not genereated. shows lot of errors. please help.
code for profile.php
<html>
<body>
<?php
// Inialize session
session_start();
// Check, if user is already login, then jump to secured page
if (!isset($_SESSION['empno'])) {
header('Location: login.php');
}
?>
<?php
include 'config.php';
include 'opendb.php';
$id = $_GET['empno'];
$query = "SELECT * FROM detail WHERE empno = '$id' LIMIT 1";
$result = mysql_query($query) or die("Error processing query. ".mysql_error());
$empno = mysql_fetch_array($result);
echo "<table width = '400' border = '1'>";
echo "<tr>";
echo "<td colspan = '2'>".$empno->empno."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>".$empno->name."</td>";
echo "<td>".$empno->branch."</td>";
echo "</tr>";
echo "</table>";
include 'closedb.php'
?>
</body>
</html>