473,734 Members | 2,806 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

88 New Member
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 2081
pbmods
5,821 Recognized Expert Expert
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
kang jia
88 New Member
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_arr ay(): 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 Recognized Expert Expert
Heya, Kang.

However when i execute the code, the page always complain that there is errror: Warning: mysql_fetch_arr ay(): 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
kang jia
88 New Member
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
4739
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 athletes.  I have now learned how to join two different tables in a query so that I might generate a report. Specifically, student athletes at a community college are required to graduate with an AA or AS degree.  Consequently, various steps are in...
8
2961
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. Now, let's say the user wants to be able to send mailings to people who have various combinations of membership and non-membership in those groups. Here's a medium-complex example: (Knitting Group or Macrame Group) and Active Contact and Mailing...
3
1827
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 that
2
1513
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 (((tblVehicleJobs.Reclaimed)=No) AND ((tblVehicleJobs.VSaleID) Is Null) AND ((tblVehicleJobs.ENF262Written)=True)); And this one produces NONE... SELECT DISTINCT tblVehicleJobs.Reclaimed, tblVehicleJobs.VSaleID,
3
1832
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 (primary ID) FirstName (text) LastName (text) BadgeIn (text) BadgeOut (text) Damaged (yes/no)
3
3512
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 form has an unbound combo box listing all the meetings in the database allowing the user to navigate between meeting records. The meetings form also has a list box that displays a list of members associated with a meeting.
3
5058
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 did it with simple text. This time around, I want to handle much more complex text. I need to preserve some kinds of features. (Yes, I have turned off magic quotes.) - Need to strip diacritics. I learned I could use htmlentities to catch...
2
2074
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; src=&quot;http://www.mysite.com/fckeditor/editor/images/smiley/msn/ regular_smile.gif&quot; /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
9
5559
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 1.5.. i have created a form bean to get the data from the user. then in my action class i m trying to get a database connection and adding the values of the user. and i also wanna show them back to him. its a dummy project for my learning(to get...
0
8776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9182
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.