Page 2 of 2

Re: search form shows all records

Posted: Sat May 03, 2008 4:14 am
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.

Re: search form shows all records

Posted: Wed May 07, 2008 7:10 am
by sirTemplar
no one to check what's wrong? :(

Re: search form shows all records

Posted: Wed May 07, 2008 7:36 am
by DeFacto

Code: Select all

 
if($_POST['$strAuthor'] == "" || $_POST['$strTitle'] == "")
{
print "An error has occured. Author and Title require input";  
}
else {
 
...............
 
}
try that way.

Re: search form shows all records

Posted: Wed May 07, 2008 11:00 am
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.

Re: search form shows all records

Posted: Wed May 07, 2008 12:14 pm
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'];
 

Re: search form shows all records

Posted: Wed May 07, 2008 12:24 pm
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?

Re: search form shows all records

Posted: Sat Jul 26, 2008 2:23 am
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

Re: search form shows all records

Posted: Sun Jul 27, 2008 2:38 pm
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);
?>

Re: search form shows all records

Posted: Tue Jul 29, 2008 1:21 am
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.

Re: search form shows all records

Posted: Tue Jul 29, 2008 12:47 pm
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.