PHP Query Assistance

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
transfield
Forum Commoner
Posts: 34
Joined: Wed Feb 22, 2006 6:00 am

PHP Query Assistance

Post by transfield »

I have 3 forms all located in 1 page. The code is below. All these 3 forms have got their form action pointing towards Untitled-3.php. The code of Untitled-3.php is also below.

Now here's what I want to achieve. I want each form to query different fields individually. Explained slightly differently, form1 should query the add_1 field, form2 should query the address field & form3 should query the price field. I've already got form1 to query the area_1 field successfully by using the code below:-

Code: Select all

$query="SELECT * FROM average_income WHERE (area_1 like '%$frmarea_1%')";
Now how do I get form2 & form3 working?

I'm a newbie to php so please be patient with me.

Thanks.

The code for the forms:-

Code: Select all

<head>
<title>Untitled Document</title>
</head>
<body>
<form name="form1" method="POST" action="Untitled-3.php">
  <p>

  <table cellspacing="0" cellpadding="0">
  <col width="64" span="3">
  <tr height="17">
    <td width="64" height="17">Area</td>
    <td width="64"></td>
    <td width="64"><input name="area_1" type="text" id="area_1"></td>
  </tr>
  <tr height="17">
    <td height="17"></td>
    <td></td>
    <td></td>
  </tr>
</table>
  <p>
    <input name="submit" type="submit" id="submit" value="Submit"> 

</form> 
<form name="form2" method="POST" action="Untitled-3.php">
<table cellspacing="0" cellpadding="0">
  <col width="64" span="3">
  <tr height="17">
    <td width="64" height="17">Address</td>
    <td width="64"></td>
    <td width="64"><input name="address2" type="text" id="address2"></td>
  </tr>
  <tr height="17">
    <td height="17"></td>
    <td></td>
    <td></td>
  </tr>
</table>
<p>
  <input name="submit2" type="submit" id="submit2" value="Submit">
  </form>
  <form name="form3" method="POST" action="Untitled-3.php">
<table cellspacing="0" cellpadding="0">
  <col width="64" span="3">
  <tr height="17">
    <td width="64" height="17">Price</td>
    <td width="64"></td>
    <td width="64"><input name="price3" type="text" id="price3"></td>
  </tr>
  <tr height="17">
    <td height="17"></td>
    <td></td>
    <td></td>
  </tr>
</table>
<p>
  <input name="submit3" type="submit" id="submit3" value="Submit">
  </form>
<p>&nbsp;</p>
</body>
</html>
The code for Untitled-3.php:-

Code: Select all

<head>
<title>Untitled Document</title>
</head>
<body>
<?php
import_request_variables("gP", "frm");
if ($frmarea_1 == '') $frmarea_1 = '0';
if ($frmaddress == '') $frmaddress = '0';
if ($frmprice == '') $frmprice = '0';

$username="abc123";
$password="abc123";
$database="abc123";
$host="localhost";

mysql_connect ("$host","$username","$password");
mysql_select_db($database) or die( "Where's the database man?");
$query="SELECT * FROM average_income WHERE (area_1 like '%$frmarea_1%')";

$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
?>

<table border="0" cellspacing="2" cellpadding="2">
<tr> 
<th><font face="Arial, Helvetica, sans-serif">Area</font></th>
<th><font face="Arial, Helvetica, sans-serif">Address</font></th>
<th><font face="Arial, Helvetica, sans-serif">Price</font></th>
</tr>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

hrmm something like this, in psuedo-code

Code: Select all

switch($form){
   case "form1";
   $field = "somefieldA";
   break;

   case "form2";
   $field = "somefieldB";
   break;

   case "form2";
   $field = "somefieldC";
}

$result = mysql_query("SELECT * FROM table WHERE $field LIKE '$form'") or die(mysql_error());
You'll want to dynamically build your query, querying the appropriate field based on which form is being filled out.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
transfield
Forum Commoner
Posts: 34
Joined: Wed Feb 22, 2006 6:00 am

Post by transfield »

Thank you for your reply, scrotaye. I tried this code but it did not work either. Here's exactly what I added into my original code:-

Code: Select all

switch($form){ 
   case "form1"; 
   $field = "area_1"; 
   break; 
   case "form2"; 
   $field = "address"; 
   break; 
   case "form3"; 
   $field = "price"; 
}
And this:-

Code: Select all

$query="SELECT * FROM average_income WHERE '$field' LIKE '$form'" or die(mysql_error());
What am I doing wrong here?

I do not understand what your meant by
You'll want to dynamically build your query
and what's
psuedo-code
please?

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

pseudo = fake, not real.

using isset(), you can check for each form being in the submission data. isset($_POST['address']) for instance. (Although I'd loop it, personally.)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

transfield wrote:
I do not understand what your meant by
You'll want to dynamically build your query
Thanks
What i mean is that you won't have a set query.. you'll need to build it based on which form is being submitted. The switch() statement could also be done in a series of if() statements, which may be easier for you to understand. Perhaps something like this:

[again, in fake-code, because i do not know your variables

Code: Select all

if($thisIsFormOne){
  // query for form one here
}
if($thisIsFormTwo){
  // query for form two here
}
if($thisIsFormThree){
  // query for form three here
}
That code will do the same as the switch() statement except it's less compact. But that doesn't matter as long as you get your code working. :)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You might be better off doing something like this:

Code: Select all

form name="form1" method="POST" action="Untitled-3.php">
  <input type="hidden" name="form_id" value="form1">

<form name="form2" method="POST" action="Untitled-3.php">
  <input type="hidden" name="form_id" value="form2">

<form name="form3" method="POST" action="Untitled-3.php">
  <input type="hidden" name="form_id" value="form3">
And then on the PHP side do:

Code: Select all

switch($_POST['form_id']){ 
   case "form1"; 
      ... 
      break; 
   case "form2"; 
      ... 
      break; 
   case "form3"; 
      ... 
      break; 
   default: 
}
You could also switch on the name of the submit button, but then if they pressed Enter in IE you would get nothing.
(#10850)
transfield
Forum Commoner
Posts: 34
Joined: Wed Feb 22, 2006 6:00 am

Post by transfield »

Thanks for your reply, feyd. For a moment, I thought pseudo was some sort of computer terminology :-)

Hi, scrotaye. Thanks a lot for your explanation. I appreciate your reply.

Dear Christopher. Your code is simple & easy to understand. Thank you very much.

Warm Regards.
Post Reply