472,988 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,988 software developers and data experts.

how to use several criteria to retrieve the specific email address in database

hi

i am doing mailinglist currently. the code in my first page is like this :
Expand|Select|Wrap|Line Numbers
  1. :
  2. <html>
  3.     <head>
  4.     <link rel="stylesheet" type="text/css" href="gallery.css" />
  5. <script language="JavaScript">
  6.  
  7. <!--
  8.  
  9. function validate_form ( )
  10. {
  11.     valid = true;
  12.  if ( document.mailinglist.years.value == "" )
  13.     {
  14.         valid = true;
  15.     }  
  16.  
  17. else if((document.mailinglist.years.value != "") &&((/^((\d{1})|(\d{2}))$/).test(mailinglist.years.value) ==false)){
  18. alert ( "your input is incorrect,please enter numerical value");
  19.         mailinglist.years.focus();
  20.         valid = false;
  21.     }
  22.  
  23.         return valid;
  24.         }
  25.         //-->
  26. </script>
  27.  
  28. </head>
  29.  
  30.     <body>
  31.     <div id="header">Mailing List
  32. </div>
  33. <div id="leftcol">    
  34. </div>
  35. <div id="maincol">
  36.     <form method = 'POST' name= 'mailinglist' action = 'mailinglist.php' onSubmit = 'return validate_form ();'>
  37.     Select any one of below options to check Mailing list<br> 
  38.         Age: 
  39.          <select name="age">
  40.                             <option value="">---</option>
  41.                             <option value="15-25">15-25years</option>
  42.                             <option value="26-35">26-35years</option>
  43.                             <option value="36-45">36-45years</option>
  44.                             <option value="46-60">46-60years</option>
  45.                             <option value="60+">60+years</option>
  46.                             </select>
  47.         </select>
  48.  
  49.         <br><br>
  50.         Gender: 
  51.         <select name = 'gender'value='3'>
  52.             <option value = ''>--</option>
  53.             <option value = 'Female'>Female</option>
  54.             <option value = 'Male'>Male</option>
  55.  
  56.         </select>
  57.         <br><br>
  58.  
  59.  
  60.         Country: 
  61.         <select name="country" tabindex="11">
  62.                             <option value="">---</option>
  63.                             <option value="Indonesia" >Indonesia  </option>
  64.                             <option value="Malaysia" >Malaysia  </option>
  65.                             <option value="Philippines" >Philippines   </option>
  66.                             <option value="Singapore" > Singapore </option>
  67.                             <option value="Thailand" > Thailand</option>
  68.                             <option value="Vietnam " > Vietnam  </option>
  69.                             <option value="Laos" >Laos </option>
  70.                             <option value="Myanmar" > Myanmar </option>
  71.                             <option value="Cambodia" > Cambodia </option>
  72.                             <option value="Brunei" > Brunei </option>
  73.         </select>
  74.         <br><br>
  75.  
  76.    <td><font size="4"color="red">*</font>write down your number of years played golf: </td> <td><input type = "text" name="years"></td>
  77.  
  78.     <tr><td><i>(Please enter in numerical figures)</i></td></tr>    
  79.                     <br><br>
  80.                     <input type = 'submit' value = 'Show Mailinglist'>
  81.         </form>
  82.         </div>
  83. </body>
  84. </html>
  85.  
i would like to let user retrieve email address of user by choosing gender alone or gender with age, gender with country or gender with years . similarly, they can also choose three criteria, for example, gender with country with age. or similarly, they can combine four criterias in order to select the specific email address. for example gender with country with age with years( this is no of years people played golf)

the coding of my next page mailinglist.php is in the following:

Expand|Select|Wrap|Line Numbers
  1. :
  2. <?
  3. //session_start() does not create new session, but identifies same session from do_login.php
  4. session_start();
  5. require("db.php");
  6.  
  7. mysql_connect(MACHINE, USER, '');
  8. mysql_select_db(DBNAME);
  9.  
  10. $gender=$_POST['gender'];
  11. $country=$_POST['country'];
  12. $age=$_POST['age'];
  13. $years=$_POST['years'];
  14.  
  15.  
  16.     if ($age){
  17.         $selEmail= "SELECT * from users where age ='".$age."'";
  18. }
  19.  
  20.     elseif($gender){
  21.         //$selEmail = "SELECT * from mailinglist";
  22.                 $selEmail= "SELECT * from users where gender ='".$gender."'";
  23.     }
  24.     if(($gender)&&($age)){
  25.         //$selEmail = "SELECT * from mailinglist";
  26.                 $selEmail= "SELECT * from users where gender ='".$gender."'"."and age ='".$age."'";
  27.                 echo $selEmail;
  28.     }
  29.  
  30.     elseif($country){
  31.         //$selEmail = "SELECT * from mailinglist";
  32.                 $selEmail= "SELECT * from users where country='".$country."'";
  33.                 }
  34.     if(($country)&&($age)){
  35.  
  36.                 $selEmail= "SELECT * from users where country ='".$country."'"."and age ='".$age."'";
  37.                 echo $selEmail;
  38.                 }
  39.     if(($country)&&($gender)){
  40.  
  41.                 $selEmail= "SELECT * from users where country ='".$country."'"."and gender='".$gender."'";
  42.                 echo $selEmail;
  43.                 }
  44.     if(($country)&&($years)){
  45.  
  46.                 $selEmail= "SELECT * from users where country ='".$country."'"."and years ='".$years."'";
  47.                 echo $selEmail;
  48.     }
  49.  
  50.     elseif($years){
  51.         //$selEmail = "SELECT * from mailinglist";
  52.                 $selEmail= "SELECT * from users where years='".$years."'";
  53.     }
  54.     if (($years)&&($age)){
  55.                $selEmail= "SELECT * from users where years='".$years."'"."and age ='".$age."'";
  56.       }
  57.      if(($years)&&($gender)){
  58.  
  59.                 $selEmail= "SELECT * from users where country ='".$country."'"."and gender ='".$gender."'";
  60.                 echo $selEmail;
  61.                 }
  62.  
  63.         $a = mysql_query($selEmail);
  64.  
  65.  
  66.     echo "<table border = '1'>";
  67.     //normal header row
  68.  
  69.     //advance header row for Q17
  70.     echo "<tr>";
  71.     echo "<td><b>Name</b></td>";
  72.     echo "<td><b>Email</b></td>";
  73.     echo "<td><b>Age</b></td>";
  74.     echo "<td><b>Gender</b></td>";
  75.     echo "<td><b>Country</b></td>";
  76.     echo "<td><b>Years Played Golf</b></td>";
  77.     //echo "<td><b>Delete?</b></td>";
  78.     //echo "<td><b>Update?</b></td>";
  79.     echo "</tr>";
  80.     //////////////////////////////////////////////////////
  81.  
  82.     while($thisEmail = mysql_fetch_array($a)){
  83.         $name=$thisEmail['name'];
  84.         $age = $thisEmail['age'];
  85.         $gender = $thisEmail['gender'];
  86.         $country = $thisEmail['country'];
  87.         $years=$thisEmail['years'];
  88.         $email=$thisEmail['email'];
  89.         echo "<tr>";
  90.         echo "<td>".$name."</td>";
  91.         echo "<td>".$email."</td>";
  92.         echo "<td>".$age."</td>";
  93.         echo "<td>".$gender."</td>";
  94.         echo "<td>".$country."</td>";
  95.         echo "<td>".$years."</td>";
  96.         //echo "<td><a href=\"do_delete.php?id=".$id."\">Delete</a></td>";
  97.         //echo "<td><a href=\"updatecontact.php?id=".$id."\">Update</a></td>";
  98.         echo "</tr>";        
  99.     }
  100.     echo "</table>";
  101.     echo"<a href=mailinglist.html>go back</a>";
  102.     echo"<br>";
  103.     echo"<a href=index.php>go back to home page</a>";
  104. ?>    
  105.  
  106.  
  107.  
  108.  
  109.  
i think my code until now can only let user use one criteria or two criteria to retrieve emailing address from database. how can i let them use three criteria and four criteria to retrieve emailing address from database so that they can get the specific email address they want.

anyone can help me with this, thanks in advance. :)
Jul 7 '07 #1
4 2031
pbmods
5,821 Expert 4TB
Heya, Kang.

Instead of creating a separate query for each case, try something like this:
Expand|Select|Wrap|Line Numbers
  1. $sql = 'SELECT * FROM `users` ';
  2. $search = '';
  3.  
  4. foreach($_POST as $field => $val)
  5.     if(! empty($val))
  6.         $search .= ($search ? ' AND' : 'WHERE') . " (`{$field}` = '{$val}')";
  7.  
  8. if(empty($search)) {
  9.     // No search criteria.  Generate an error.
  10.     .
  11.     .
  12.     .
  13. } else {
  14.     // Execute the search and process the results.
  15.     $result = mysql_query($sql . $search);
  16.     .
  17.     .
  18.     .
  19. }
  20.  
This will run through ALL values in $_POST and append them to the search query.
Jul 9 '07 #2
Heya, Kang.

Instead of creating a separate query for each case, try something like this:
Expand|Select|Wrap|Line Numbers
  1. $sql = 'SELECT * FROM `users` ';
  2. $search = '';
  3.  
  4. foreach($_POST as $field => $val)
  5.     if(! empty($val))
  6.         $search .= ($search ? ' AND' : 'WHERE') . " (`{$field}` = '{$val}')";
  7.  
  8. if(empty($search)) {
  9.     // No search criteria.  Generate an error.
  10.     .
  11.     .
  12.     .
  13. } else {
  14.     // Execute the search and process the results.
  15.     $result = mysql_query($sql . $search);
  16.     .
  17.     .
  18.     .
  19. }
  20.  
This will run through ALL values in $_POST and append them to the search query.


hi
thanks your kind reply, now my code is
Expand|Select|Wrap|Line Numbers
  1. <?
  2. session_start();
  3. require("db.php");
  4.  
  5. mysql_connect(MACHINE, USER, '');
  6. mysql_select_db(DBNAME);
  7.  
  8. $gender=$_POST['gender'];
  9. $country=$_POST['country'];
  10. $age=$_POST['age'];
  11. $years=$_POST['years'];
  12. $sql="SELECT * from users";
  13. $search='';
  14. foreach($_POST as $field=>$val)
  15. if (!empty ($val))
  16. $search.=($search ? 'AND':'WHERE')."('{$field}'='{$val}')";
  17. if (empty($search)){
  18. //no search criteria, Generate an error.
  19. echo "<script>alert('you did not select any criteria to view email list ')</script>";
  20.     }
  21. else{
  22. //execute the search and process the results
  23. $result=mysql_query($sql.$search);
  24. }
  25.     echo "<table border = '1'>";
  26.     //normal header row
  27.  
  28.     //advance header row for Q17
  29.     echo "<tr>";
  30.     echo "<td><b>Name</b></td>";
  31.     echo "<td><b>Email</b></td>";
  32.     echo "<td><b>Age</b></td>";
  33.     echo "<td><b>Gender</b></td>";
  34.     echo "<td><b>Country</b></td>";
  35.     echo "<td><b>Years Played Golf</b></td>";
  36.     //echo "<td><b>Delete?</b></td>";
  37.     //echo "<td><b>Update?</b></td>";
  38.     echo "</tr>";
  39.     //////////////////////////////////////////////////////
  40.  
  41.     while($thisEmail = mysql_fetch_array($result)){
  42.         $name=$thisEmail['name'];
  43.         $age = $thisEmail['age'];
  44.         $gender = $thisEmail['gender'];
  45.         $country = $thisEmail['country'];
  46.         $years=$thisEmail['years'];
  47.         $email=$thisEmail['email'];
  48.         echo "<tr>";
  49.         echo "<td>".$name."</td>";
  50.         echo "<td>".$email."</td>";
  51.         echo "<td>".$age."</td>";
  52.         echo "<td>".$gender."</td>";
  53.         echo "<td>".$country."</td>";
  54.         echo "<td>".$years."</td>";
  55.         //echo "<td><a href=\"do_delete.php?id=".$id."\">Delete</a></td>";
  56.         //echo "<td><a href=\"updatecontact.php?id=".$id."\">Update</a></td>";
  57.         echo "</tr>";        
  58.     }
  59.     echo "</table>";
  60.     echo"<a href=mailinglist.html>go back</a>";
  61.     echo"<br>";
  62.     echo"<a href=index.php>go back to home page</a>";
  63. ?>    
  64.  
However when i execute the code, the page always complain that there is errror: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource. i don;t know where is wrong with this while loop.

PS. if you don;t mind, can explain to me of the code:
Expand|Select|Wrap|Line Numbers
  1.       $search .= ($search ? ' AND' : 'WHERE') . " (`{$field}` = '{$val}')";
  2.  
from my understand i know you are doing sql select query, but where does "?" ":" and {$field} means? in fact, this is the first time i see this kind of selection. As i have to do my presentation to the judge, so i also have to explain to them of the meaing. thank you very much :) if possible, can reply me ASAP, as the project's deadline is very near. a bit urgent of the time now.
Jul 10 '07 #3
pbmods
5,821 Expert 4TB
Heya, Kang.

However when i execute the code, the page always complain that there is errror: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource. i don;t know where is wrong with this while loop.
There's something wrong with the query. Change line 23:
Expand|Select|Wrap|Line Numbers
  1. if(! ($result = mysql_query($sql . $search))
  2.     exit($sql . $search . "\n" . mysql_error());
  3.  
This will set $result, and if it is invalid will tell you what is wrong.

PS. if you don;t mind, can explain to me of the code:
Expand|Select|Wrap|Line Numbers
  1.       $search .= ($search ? ' AND' : 'WHERE') . " (`{$field}` = '{$val}')";
  2.  
from my understand i know you are doing sql select query, but where does "?" ":" and {$field} means? in fact, this is the first time i see this kind of selection. As i have to do my presentation to the judge, so i also have to explain to them of the meaing. thank you very much :)
Sure. The '? :' is part of PHP's ternary operator. What we're doing in that statement is prepending 'WHERE' if we're adding the first search term. Otherwise, we'll prepend 'AND' instead.

As far as putting '{}' around variables in strings, it's not strictly necessary, but I like to do it to make my code easier to read (for me anyway). It's known as complex syntax.
Jul 10 '07 #4
Heya, Kang.



There's something wrong with the query. Change line 23:
Expand|Select|Wrap|Line Numbers
  1. if(! ($result = mysql_query($sql . $search))
  2.     exit($sql . $search . "\n" . mysql_error());
  3.  
This will set $result, and if it is invalid will tell you what is wrong.



Sure. The '? :' is part of PHP's ternary operator. What we're doing in that statement is prepending 'WHERE' if we're adding the first search term. Otherwise, we'll prepend 'AND' instead.

As far as putting '{}' around variables in strings, it's not strictly necessary, but I like to do it to make my code easier to read (for me anyway). It's known as complex syntax.

thank you very much, now it works, if we go into a bit deep, how can i alter users if database don;t have their selected criteria for the email. thanks
Jul 11 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Sami | last post by:
I hope someone will tell me how to do this without having to do any VB as I know nothing in that area. I am a rank beginner in using Access. I have created a database consisting of student...
8
by: Steve Jorgensen | last post by:
Mailing List management is a good example of a case where my conundrum arises. Say there is a m-m relationship between parties and groups - anyone can be a member of any combintation of groups. ...
3
by: Richard Thornley | last post by:
Hello, I was just been given a project and I have some questions on how to accomplish the first part of the task. If a user sends an email to a specific email address I need to detect...
2
by: MLH | last post by:
Why does this one produce 9 records... SELECT DISTINCT tblVehicleJobs.VehicleJobID, tblVehicleJobs.Reclaimed, tblVehicleJobs.VSaleID, tblVehicleJobs.ENF262Written FROM tblVehicleJobs WHERE...
3
by: starke1120 | last post by:
I need to write a query that has a criteria of two separate fields (one compared to another, then a seperate one) but I can't figure out how to do it. Let me first explain the table id...
3
by: rhobson2 | last post by:
Hello, I wrote a database applicaiton using Access XP (2002) and everything has been working good for the client until they purchased a couple of new computers with Access 2003. The meetings...
3
by: Alfred | last post by:
I want to post text field data from these HTML TEXTAREA tags to a PostgreSQL database and have it reappear back on another page exactly as I had typed it. Over the years I have done this but only...
2
by: zek2005 | last post by:
Hi! I have a value loaded in a database as following: &lt;p&gt;&lt;strong&gt;test&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;test 2&lt;/ strong&gt;&lt;em&gt;&lt;strong&gt;questions&lt;img alt=&quot;&quot;...
9
by: gavy7210 | last post by:
hello friends i am new to struts. i want to add the user information in the database and then display it back to him. i am using struts 1.2,Eclipse Platform Version: 3.4.2,mySql 5.0.1,jdk...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.