[SOLVED] search form shows all records

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: search form shows all records

Post by RobertGonzalez »

Logic:

Code: Select all

IF the form is posted THEN
  IF the required fields are not empty THEN
    EXECUTE the query and show results
  ELSE
    SHOW the user what was wrong
    SHOW the form
  END IF
ELSE 
  SHOW the form
END IF
 
Now go code that.
sirTemplar
Forum Commoner
Posts: 65
Joined: Wed Dec 18, 2002 1:57 am

Re: search form shows all records

Post by sirTemplar »

no one to check what's wrong? :(
DeFacto
Forum Commoner
Posts: 37
Joined: Wed Apr 23, 2008 2:30 pm

Re: search form shows all records

Post by DeFacto »

Code: Select all

 
if($_POST['$strAuthor'] == "" || $_POST['$strTitle'] == "")
{
print "An error has occured. Author and Title require input";  
}
else {
 
...............
 
}
try that way.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: search form shows all records

Post by RobertGonzalez »

DeFacto wrote:

Code: Select all

 
if($_POST['$strAuthor'] == "" || $_POST['$strTitle'] == "")
{
print "An error has occured. Author and Title require input";  
}
else {
 
...............
 
}
try that way.
That's not going to work. You quoted your array index variables.
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: search form shows all records

Post by kryles »

umm you also don't use $ in the quotes for post variables from what I recall.

Code: Select all

 
$_POST['variable'] isn't the same as $_POST['$variable'];
 
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: search form shows all records

Post by RobertGonzalez »

sirTemplar wrote:no one to check what's wrong? :(
Have you written code that is in line with the pseudo code I posted earlier?
sirTemplar
Forum Commoner
Posts: 65
Joined: Wed Dec 18, 2002 1:57 am

Re: search form shows all records

Post by sirTemplar »

it's me again.
from the examples above, i had this for my action code

Code: Select all

{$Author = $_POST['Author'];}  
{$Title = $_POST['Title'];}  
 
if($_POST['Author'] == "" || $_POST['Title'] == "")
{
print "An error has occured. Author and Title require input";
  
} 
 
else 
{
$result = mysql_query ("SELECT * FROM catalogue
                       WHERE Author LIKE '%$Author%'
                       AND Title LIKE '%$Title%'
                       ORDER BY Title ASC, Author
                      ",$conn);  
 
$totalrows = mysql_num_rows($result);
 
if ($row = mysql_fetch_array($result)) {  
 
do {  
now it's not printing ALL anymore when there is no user input on the search form BUT now to search user must input on both fields! what i want is that AT LEAST they input on TITLE or AUTHOR field. Thanks for any help
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: search form shows all records

Post by RobertGonzalez »

Code: Select all

<?php
$Author = $_POST['Author'];
$Title = $_POST['Title'];
 
if (empty($Author) && empty($Title)) {
  print "An error has occured. Either Author or Title must be entered";
  exit;
}
 
$sql = 'SELECT * FROM catalogue
                       WHERE ';
 
if ($Author) {
  $sql .= 'Author LIKE \'%' . mysql_real_escape_string($Author) . '%\'';
}
 
if ($Title) {
  if ($Author) {
    $sql .= ' AND ';
  }
  $sql .= 'Title LIKE \'%' . mysql_real_escape_string($Title) . '%\'';
}
 
$sql .= 'ORDER BY Title ASC, Author';
 
if (!$result = mysql_query($sql)) {
  die('Query failed: ' . mysql_error());
}
 
$totalrows = mysql_num_rows($result);
?>
sirTemplar
Forum Commoner
Posts: 65
Joined: Wed Dec 18, 2002 1:57 am

Re: search form shows all records

Post by sirTemplar »

thanks for the response again Everah. i resolved the isse by just substituting || with && on my previous code. thanks however. i have another issue but i will post it on a different topic now. btw how can i put solved on this post. it may help others too.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: search form shows all records

Post by RobertGonzalez »

Glad I could help.

To mark this as solved, go to your original post and edit the title by adding [SOLVED] in front of it.
Post Reply