Hi guys I'm new here so I apologise if this is the wrong place to 'submit my query' pardon the pun.
I'm new to PHP, in fact I've been coding with it now for a grand amount of 1 week. I understand the basics and I've managed to produce a simple one-field working form that inputs into my PHP page and queries my database.
I did this with the following code (I've changed the names of my host, database and fields within the database:
//query.php
<?
// make connection to server.
$dbServer=mysql_connect("host","database","pass");
// inserted error code to be echoed if failed to connect to server.
if (!$dbServer) {echo "Failed to connect to MySQL"; exit; }
// select the database.
mysql_select_db("database",$dbServer);
// query.
$sql ="SELECT * FROM table";
$sql.=" WHERE fieldone LIKE\"%".$_POST["fieldone"]."%\"";
echo $sql;
$queryResult=mysql_query($sql);
// error message in case of error during query.
if (mysql_error())
{
echo "Problem with Query<BR>";
echo "The following error message was returned from MySQL:<BR>";
echo mysql_error();
exit;
}
// response to query.
if (mysql_num_rows($queryResult)==0)
{
echo "Your search returned no results.";
}
else
{
while ($dbRecord=mysql_fetch_array($queryResult))
{
echo "<BR>found: <BR>".$dbRecord["colone"].", ".$dbRecord["coltwo"].", ".$dbRecord["colthree"].", ".$dbRecord["colfour"].", ".$dbRecord["colfive"].", ".$dbRecord["colsix"].", ".$dbRecord["colsev"]."<BR>";
}
}
?>
That worked, yey...but that's not all, the search form on my website does not just have one input field "fieldone"; it has 6, so for now if we just call them:
fieldone
fieldtwo
fieldthree
fieldfour
fieldfive
fieldsix
fieldone to fieldfive are text fields and I want them to be "LIKE" their database counterparts, but fieldsix is a year such as 1950. I want fieldsix to be equal (=\"".$_POST["fieldsix"]."\""; ) to its database counterpart.
Well, as you guessed I want the user of my website to go onto the search page and be able to use any number (at least 1 of course) of the fields as they want to search for the product they're looking for.
I've been working so much on this but I just can't crack it. My main problem is I can't see my PHP working because I can't access my database from home so I'm blindly coding here without much of a clue what its doing. I'd appreciate any help you can offer.
Just to give you a better idea of my code here's a few snippets I've been playing with trying to get to work:
// connection to server.
$dbServer=mysql_connect("host","database","pass");
// error code if failed to connect.
if (!$dbServer) {echo "Failed to connect to MySQL"; exit; }
// select the database.
mysql_select_db("database",$dbServer);
// query.
$sql ="SELECT * FROM table";
if (isset ($_POST["fieldone"])) //if fieldone is set
{
if (($_POST["fieldone"])!="") //and if fieldone !=""
{
$sql.=" WHERE fieldone LIKE \"%".$_POST["fieldone"]."%\" AND ";
}
else
{
$sql.=" WHERE ";
}
}
if (($_POST["fieldtwo"])!="")
{
$sql.=" fieldtwo LIKE \"%".$_POST["fieldtwo"]."%\" AND ";
}
if (($_POST["fieldthree"])!="")
{
$sql.=" fieldthree LIKE \"%".$_POST["fieldthree"]."%\"";
}
if (($_POST["fieldfour"])!="")
{
$sql.=" fieldfour LIKE \"%".$_POST["fieldfour"]."%\" AND ";
}
if (($_POST["fieldfive"])!="")
{
$sql.=" fieldfive LIKE \"%".$_POST["fieldfive"]."%\" AND ";
}
if (($_POST["fieldsix"])!="")
{
$sql.=" fieldsix=\"".$_POST["fieldsix"]."\"";
}
echo $sql;
$queryResult=mysql_query($sql);
// error message in case of error during query.
if (mysql_error())
{
echo "Problem with Query<BR>";
echo "The following error message was returned from MySQL:<BR>";
echo mysql_error();
exit;
}
// response to query
if (mysql_num_rows($queryResult)==0)
{
echo "your search returned no results.";
}
else
{
while ($dbRecord=mysql_fetch_array($queryResult))
{
echo "<BR>found: <BR>".$dbRecord["colone"].", ".$dbRecord["coltwo"].", ".$dbRecord["colthree"].", ".$dbRecord["colfour"].", ".$dbRecord["colfive"].", ".$dbRecord["colsix"].", ".$dbRecord["colsev"]."<BR>";
}
}
?>
Well, that's about it really, I think you know what I'm trying to do but it's just not producing what I want it to.
Bascially I just want:
The search criteria from searchpage.htm to be queried into the database and posted onto query.php.
I want empty fields from the search to be ignored and the used search fields somehow still need to be ANDed together if you know what I mean.
Thanks in advance for any help anybody can offer,
regards
Bigfatcat