array issue

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

Post Reply
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

array issue

Post by michaelk46 »

Hey all,

I am trying to setup a MySQL database search based on two choices either page id or by the page name.... It's supposed to populate the search string if the corresponding box is set, but it's not working.
Regardless of whether or not the boxes are populated, it puts the corresponding options into the string. Here is an example of if you hit the search button when both boxes are blank...

"SELECT * FROM pages WHERE searchid = and pagename = "

It should read like this:
"SELECT * FROM pages WHERE"

Actually it should just drop by to the search page, but one step at a time.

Here is the code:
if ($_SERVER['REQUEST_METHOD'] != "POST")
{
include 'search.html.php';
}
else
{
$searcharray = array();
$a = 0;
$searchid = NULL;
$pagename = NULL;
$searcharray[$a] = ('SELECT * FROM pages WHERE');
$a++;
$searchid = $_REQUEST['searchid'];
$pagename = $_REQUEST['pagename'];
if (isset($_REQUEST['searchid']))
{
$searcharray[$a] = ('searchid = ' . $searchid);
}

if (isset($_REQUEST['searchid'], $_REQUEST['pagename']))
{
$a++;
$searcharray[$a] = ('and ');
$a++;
}

if (isset($pagename))
{
$searcharray[$a] = ('pagename = ' . $_REQUEST['pagename']);
}

$sql = join('', $searcharray);
include 'searchres.html.php';
}

Just as a heads up, currently searchres.html.php is an echo statement to output the $sql string currently.

ANy help on why this is occuring would be great.... Thanks all.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: array issue

Post by Weiry »

Ok to be frank... why are you using arrays to generate your SQL?
To me it seems kinda pointless. You could just as easily do this:

Code: Select all

 
if(!isset($_POST)){
   include_once("search.html.php");
}else{
   $sql = "SELECT * FROM `pages`";
   if(isset($_POST['searchid']) || isset($_POST['pagename'])){
      $sql .= " WHERE ";
   }
 
   if(isset($_POST['searchid'])){
      $sql .= "`searchid` = {$_POST['searchid']} ";
   }elseif(isset($_POST['pagename'])){
      $sql .= "`pagename` = {$_POST['pagename']} ";
   }else{
      $sql .= "`searchid` = {$_POST['searchid']} AND `pagename` = {$_POST['pagename']}";
   }
 
   include_once("searchres.html.php");
}
 
Or something similar at least.
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: array issue

Post by michaelk46 »

Well,

I definetly appreciate your reply, I am new to PHP and this is the first way I learned how to do it. I am always looking for a better way to program, and this is greatley appreciated

thanks again
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: array issue

Post by michaelk46 »

I tried out your solution and am getting the same problem, regardless of what's typed into either box ti outputs

SELECT * FROM `pages` WHERE `searchid` =

If I type something into the searchid box it will print the correct info in the correct space, but nothing else.
Post Reply