Search database/Page Results
Moderator: General Moderators
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
You might want to consider setting a $_SESSION['search_lname'] var and setting that to '' (empty). When there is something passed in the form as a search parameter, assign that session var to the search param so it stays with the paging routine (just make sure to not set up paging when the search term is null or you might end up pulling all 3 million records).
I was able to create the session variable to use in the next page... no error messages. But my query is not pulling the desired results for some reason.
It's just doing a Select * but not using the Where condition...
Code: Select all
$query = "SELECT * from users WHERE LastName='{$_SESSION['lname']}';- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
For error checking, do this...
Code: Select all
<?php
echo 'The sessioned last name is: ' . $_SESSION['lname'];
$query = "SELECT * from users WHERE LastName='" . $_SESSION['lname'] . "'";
?>This is almost eerie - I just did that...I typed an echo statement on the original submitting page
It seemed like it was generating the correct Last Name on that page but not any more - no names are getting printed. I put in an echo statement on the page that it is getting submitted to as well and the name isn't showing up there too.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
I thought i had it displaying correctly but it wasn't. Right now, my echo statements on both search4.php and search5.php are not displaying anything.
The following is my code on 'search4.php', the page that has the form/Search field. Even the "Hello" isn't printed on the screen so i know now that the else part of my code doesn't work:
On the page processing the form and running the queries 'search5.php', i access this session variable this way:
And in my queries on that page, i refer to it this way:
Should i do it the way you've suggested?
The following is my code on 'search4.php', the page that has the form/Search field. Even the "Hello" isn't printed on the screen so i know now that the else part of my code doesn't work:
Code: Select all
<?
if (!isset($_POST['submit'])) {
//do nothing
}
else {
$_SESSION['PatientLastName'] = $_POST['PatientLastName'];
echo "Hello";
echo $_SESSION['PatientLastName'];
}
?>Code: Select all
$_SESSION['PatientLastName']Code: Select all
$query = "SELECT * from users WHERE LastName='{$_SESSION['lname']}';Code: Select all
$query = "SELECT * from users WHERE LastName='" . $_SESSION['lname'] . "'";- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
search4.php:
The form part alone is HTML...The rest of the HTML doesn't have any code of significance - just a table tag for the form.
Code: Select all
<?
echo "<center><a href='logout.php'> Logout </a></center>";
?>
<form action="search5.php" method="post" >
<p align="center">Search for User by Last Name</p>
<p align="center">
<input type="text" name="LastName">
<input type="hidden" name="test" value="test">
</p>
<p align="center">
<input type="submit" name="submit" value="Search">
</p>
</form>
<?
if (!isset($_POST['submit'])) {
//do nothing
}
else {
$_SESSION['LastName'] = $_POST['LastName'];
echo "Hello";
echo $_SESSION['LastName'];
}
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Sounds funny, but this could be a 'short tags' issue. Try using this code. Also, I see no call to session_start(). Where is that happening? It would really help to see all the code (we don't database details, but the complete code is always helpful in helping you).
Code: Select all
<?php
echo "<center><a href='logout.php'> Logout </a></center>";
?>
<form action="search5.php" method="post" >
<p align="center">Search for User by Last Name</p>
<p align="center">
<input type="text" name="LastName">
<input type="hidden" name="test" value="test">
</p>
<p align="center">
<input type="submit" name="submit" value="Search">
</p>
</form>
<?php
if (!isset($_POST['submit'])) {
//do nothing
} else {
$_SESSION['LastName'] = $_POST['LastName'];
echo "Hello";
echo $_SESSION['LastName'];
}
?>No problem. Sorry about that...
Code: Select all
<?
session_start();
include "include/db.php";
if (!isset($_SESSION['username']))
{
print "not logged in";
print "<script>";
print " self.location='login.php';";
print "</script>";
}
else
{
?>
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>My Test</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.style1 {
font-size: 16px;
font-weight: bold;
}
.input
{
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
}
.style2 {font-size: 12px}
-->
</style>
</head>
<body>
<table class="tmpl_brdrclr" width="100%" border="0" cellspacing="1" cellpadding="1" summary=" " >
<!--TABLE A - BIG-->
<tr>
<td width="125" class="tmpl_sidebackgroundl" valign="top"> </td>
<!--Content Area Begins Here-->
<td valign="top" colspan="2"> <p align="center" class="style1">Monitoring Program</p>
<?
echo "<center><a href='logout.php'> Logout </a></center>";
$display = 10;
?>
<form action="search5.php" method="post" >
<p align="center">Search for User by Last Name</p>
<p align="center">
<input type="text" name="LastName">
<input type="hidden" name="test" value="test">
</p>
<p align="center">
<input type="submit" name="submit" value="Search">
</p>
</form>
<?
if (!isset($_POST['submit'])) {
//do nothing
}
else {
$_SESSION['LastName'] = $_POST['LastName'];
echo "Hello";
echo $_SESSION['LastName'];
}
?>
</td>
<td class="tmpl_sidebackgroundl" width="158" valign="top" > </td>
</tr>
</table>
<?
} // if session started
?>
</body>
</html>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
I cleared the cache. The print_r statement's not doing anything. I inserted it just before assigning the post data to the session variable:
I've never worked with print_r - do we view the page source on submitting the form?
Code: Select all
<?php
if (isset($_POST['submit'])) {
print_r($_POST);
$_SESSION['PatientLastName'] = $_POST['PatientLastName'];
echo "Hello";
echo $_SESSION['PatientLastName'];
}
else {
//do nothing
echo "Goodbye";
}
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
print_r() will show you a lot of information about your variable(s) that you put into it. If you are getting nothing on a print_r($_POST) then your form information is not sending.