still struggling - please help

Thread Solved

Join Date: Jan 2009
Posts: 13
Reputation: phpNewbie is an unknown quantity at this point 
Solved Threads: 0
phpNewbie phpNewbie is offline Offline
Newbie Poster

still struggling - please help

 
0
  #1
Mar 2nd, 2009
i'm still struggling with php - its soooo confusing to me

this is the code i have now - is it in the wrong order? or whats missing please to make it work - i'm trying to get a result if 'both' genders are selected and if 'any' origin is selected

thanks in advance for any help

  1. <html>
  2. <head>
  3. </head>
  4. <body>
  5. <?php
  6. // MySQL Connection Information
  7. $server = "localhost";
  8. $username = "sharlene";
  9. $password = "";
  10. $dbname = "baby_names";
  11.  
  12. // MySQL Connect String
  13. $connection = mysql_connect($server,$username,$password) or die("Can't Connect to Mysql Server:
  14.  
  15. ".mysql_error());
  16.  
  17. // Select the database
  18. mysql_select_db($dbname) or die("Can't connect to database: ".mysql_error());
  19.  
  20. $gender = mysql_real_escape_string($_POST['gender']);
  21. $meaning = mysql_real_escape_string($_POST['meaning']);
  22. $name = mysql_real_escape_string($_POST['name']);
  23. $origin = mysql_real_escape_string($_POST['origin']);
  24.  
  25.  
  26.  
  27. $sql = "SELECT * FROM names WHERE gender = '" . $gender . "' and name LIKE '" . $name . "%' and
  28. meaning LIKE '%" . $meaning . "%' and origin = '" . $origin . "'";
  29.  
  30.  
  31. if ($gender == 'both') { // no specific gender
  32. if ($origin == 'any') { // no specific origin
  33. $sql = 'select * from names';
  34. else { // an origin was specified
  35. $sql = "select * from names where origin = '".$origin."'";
  36. }
  37.  
  38. else { // a gender was specified
  39.  
  40. if ($origin == 'any') { // no specific origin
  41. $sql = "select * from names where gender ='".$gender."'";
  42. else { // an origin was also specified
  43. $sql = "select * from names where origin = '".$origin."' and gender ='".$gender."'";
  44. }
  45.  
  46.  
  47. //execute SQL query and get result
  48. $sql_result = mysql_query($sql, $connection) or die(mysql_error());
  49.  
  50. ?>
  51. <table border="0" align="center">
  52. <tr>
  53. <th style="background: #ffd" >Name</th>
  54. <th style="background: #efe">Gender</th>
  55. <th style="background: #fef">Origin</th>
  56. <th style="background: #e3e4fa">Meaning</th>
  57. </tr>
  58.  
  59. <?php
  60. // Loop through the data set and extract each row into it's own variable set
  61. while ($row = mysql_fetch_array($sql_result))
  62. {
  63. //extract($row);
  64. ?>
  65. <tr>
  66. <td style="background: #ffd"><?php echo $row['name'];?></td>
  67. <td style="background: #efe"><?php echo $row['gender'];?></td>
  68. <td style="background: #fef"><?php echo $row['origin'];?></td>
  69. <td style="background: #e3e4fa"><?php echo $row['meaning'];?></td>
  70. </tr>
  71. <?php
  72. }
  73.  
  74. ?>
  75. </body>
  76. </html>

my form is:

  1. <!--html page-->
  2. <html>
  3. <head>
  4. <title>Baby Names Database</title>
  5. </head>
  6. <body>
  7. <h1>Baby Names</h1>
  8. <h3>Please fill in the blanks below, and We'll return baby names for you to browse.</h3>
  9.  
  10.  
  11. <table border = "1" align = "center">
  12. <form method = "post" action = "babyNames1.php">
  13. <tr bgcolor="#cae1ff">
  14. <td align = "left">
  15. Gender:
  16. </td>
  17. <td>
  18. <input type = "radio" name = "gender" value = "male"/>Boys Names
  19. <input type = "radio" name = "gender" value = "female"/>Girls Names
  20. <input type = "radio" name = "gender" value = ""/>Both
  21. </td>
  22. </tr>
  23. <tr bgcolor="#ffe1ff">
  24. <td align = "left">
  25. Meaning:
  26. </td>
  27. <td>
  28. <input type = "text" name = "meaning" value = ""/>
  29. </td>
  30. </tr>
  31. <tr bgcolor="#ffe1ff">
  32. <td align = "left">
  33. Name Begins With:
  34. </td>
  35. <td>
  36. <input type = "text" name = "name" value = ""/>
  37. </td>
  38. </tr>
  39. <tr bgcolor="#cae1ff">
  40. <td align = "left">
  41. Origin
  42. </td>
  43. <td>
  44.  
  45. <select name = "origin" id = "origin">
  46. <option value = "any">Any</option>
  47. <option value = "african">African</option>
  48. <option value = "anglo">Anglo</option>
  49. <option value = "arabian">Arabian</option>
  50. <option value = "arabic">Arabic</option>
  51. <option value = "aramaic">Aramaic</option>
  52. <option value = "armenian">Armenian</option>
  53. <option value = "arthurian">Arthurian</option>
  54. <option value = "basque">Basque</option>
  55. <option value = "celtic">Celtic</option>
  56. <option value = "chamoru">Chamoru</option>
  57. </select>
  58. </td>
  59. </tr>
  60.  
  61. </table><p /><center>
  62. <input type = "submit" value = "Show me Names"/>
  63. </center>
  64. </form>
  65. </body>
  66. </html>
  67.  
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: still struggling - please help

 
0
  #2
Mar 2nd, 2009
I think the problem with the 'both' option is that when you select 'both' on the form, gender is set to '' and not to 'both'.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,449
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 135
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: still struggling - please help

 
0
  #3
Mar 3rd, 2009
BTW, just to give you an idea of where Murtan is talking about (not very obvious) it is the following line of the second file where value=
  1. <input type = "radio" name = "gender" value = ""/>Both
As you can see there is no value assigned to that field and that is why it will appear as empty in the database.
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 28
Reputation: vinothkumarc is an unknown quantity at this point 
Solved Threads: 3
vinothkumarc vinothkumarc is offline Offline
Light Poster

Re: still struggling - please help

 
1
  #4
Mar 3rd, 2009
  1. <html>
  2. <head>
  3. </head>
  4. <body>
  5. <?php
  6. // MySQL Connection Information
  7. $server = "localhost";
  8. $username = "sharlene";
  9. $password = "";
  10. $dbname = "baby_names";
  11.  
  12. // MySQL Connect String
  13. $connection = mysql_connect($server,$username,$password) or die("Can't Connect to Mysql Server:
  14.  
  15. ".mysql_error());
  16.  
  17. // Select the database
  18. mysql_select_db($dbname) or die("Can't connect to database: ".mysql_error());
  19.  
  20. $gender = mysql_real_escape_string($_POST['gender']);
  21. $meaning = mysql_real_escape_string($_POST['meaning']);
  22. $name = mysql_real_escape_string($_POST['name']);
  23. $origin = mysql_real_escape_string($_POST['origin']);
  24. if($gender) {
  25. $whereArr[] = "gender = '" . $gender . "' ";
  26. }
  27. if($name) {
  28. $whereArr[] = "name LIKE '" . $name . "%'";
  29. }
  30. if($meaning) {
  31. $whereArr[] = "meaning LIKE '%" . $meaning . "%'";
  32. }
  33. if($origin) {
  34. $whereArr[] = "origin = '" . $origin . "' ";
  35. }
  36. if(count($whereArr)) {
  37. $where = @implode(" AND ", $whereArr);
  38. $where = ' WHERE '.$where;
  39. }
  40.  
  41. $sql = "SELECT * FROM names $where";
  42.  
  43. /*
  44. if ($gender == 'both') { // no specific gender
  45.   if ($origin == 'any') { // no specific origin
  46.   $sql = 'select * from names';
  47.   else { // an origin was specified
  48.   $sql = "select * from names where origin = '".$origin."'";
  49.   }
  50.  
  51. else { // a gender was specified
  52.  
  53.   if ($origin == 'any') { // no specific origin
  54.   $sql = "select * from names where gender ='".$gender."'";
  55.   else { // an origin was also specified
  56.   $sql = "select * from names where origin = '".$origin."' and gender ='".$gender."'";
  57. }
  58. /**/
  59.  
  60. //execute SQL query and get result
  61. $sql_result = mysql_query($sql, $connection) or die(mysql_error());
  62.  
  63. ?>
  64. <table border="0" align="center">
  65. <tr>
  66. <th style="background: #ffd" >Name</th>
  67. <th style="background: #efe">Gender</th>
  68. <th style="background: #fef">Origin</th>
  69. <th style="background: #e3e4fa">Meaning</th>
  70. </tr>
  71.  
  72. <?php
  73. // Loop through the data set and extract each row into it's own variable set
  74. while ($row = mysql_fetch_array($sql_result))
  75. {
  76. //extract($row);
  77. ?>
  78. <tr>
  79. <td style="background: #ffd"><?php echo $row['name'];?></td>
  80. <td style="background: #efe"><?php echo $row['gender'];?></td>
  81. <td style="background: #fef"><?php echo $row['origin'];?></td>
  82. <td style="background: #e3e4fa"><?php echo $row['meaning'];?></td>
  83. </tr>
  84. <?php
  85. }
  86.  
  87. ?>
  88. </body>
  89. </html>
Last edited by peter_budo; Mar 8th, 2009 at 6:45 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Thanks & Regards,
VinothKumar C
post2vinoth@gmail.com
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,746
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 330
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: still struggling - please help

 
0
  #5
Mar 3rd, 2009
Originally Posted by vinothkumarc View Post
<html>
<head>
</head>
<body>
<?php
// MySQL Connection Information
$server = "localhost";
$username = "sharlene";
$password = "";
$dbname = "baby_names";

// MySQL Connect String
$connection = mysql_connect($server,$username,$password) or die("Can't Connect to Mysql Server:

".mysql_error());

// Select the database
mysql_select_db($dbname) or die("Can't connect to database: ".mysql_error());

$gender = mysql_real_escape_string($_POST['gender']);
$meaning = mysql_real_escape_string($_POST['meaning']);
$name = mysql_real_escape_string($_POST['name']);
$origin = mysql_real_escape_string($_POST['origin']);
if($gender) {
$whereArr[] = "gender = '" . $gender . "' ";
}
if($name) {
$whereArr[] = "name LIKE '" . $name . "%'";
}
if($meaning) {
$whereArr[] = "meaning LIKE '%" . $meaning . "%'";
}
if($origin) {
$whereArr[] = "origin = '" . $origin . "' ";
}
if(count($whereArr)) {
$where = @implode(" AND ", $whereArr);
$where = ' WHERE '.$where;
}

$sql = "SELECT * FROM names $where";

/*
if ($gender == 'both') { // no specific gender
if ($origin == 'any') { // no specific origin
$sql = 'select * from names';
else { // an origin was specified
$sql = "select * from names where origin = '".$origin."'";
}

else { // a gender was specified

if ($origin == 'any') { // no specific origin
$sql = "select * from names where gender ='".$gender."'";
else { // an origin was also specified
$sql = "select * from names where origin = '".$origin."' and gender ='".$gender."'";
}
/**/

//execute SQL query and get result
$sql_result = mysql_query($sql, $connection) or die(mysql_error());

?>
<table border="0" align="center">
<tr>
<th style="background: #ffd" >Name</th>
<th style="background: #efe">Gender</th>
<th style="background: #fef">Origin</th>
<th style="background: #e3e4fa">Meaning</th>
</tr>

<?php
// Loop through the data set and extract each row into it's own variable set
while ($row = mysql_fetch_array($sql_result))
{
//extract($row);
?>
<tr>
<td style="background: #ffd"><?php echo $row['name'];?></td>
<td style="background: #efe"><?php echo $row['gender'];?></td>
<td style="background: #fef"><?php echo $row['origin'];?></td>
<td style="background: #e3e4fa"><?php echo $row['meaning'];?></td>
</tr>
<?php
}

?>
</body>
</html>
Use code tags for FS.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 28
Reputation: vinothkumarc is an unknown quantity at this point 
Solved Threads: 3
vinothkumarc vinothkumarc is offline Offline
Light Poster

Re: still struggling - please help

 
0
  #6
Mar 3rd, 2009
hey... y u copied... and pasted without any comments (my code)
Thanks & Regards,
VinothKumar C
post2vinoth@gmail.com
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 13
Reputation: phpNewbie is an unknown quantity at this point 
Solved Threads: 0
phpNewbie phpNewbie is offline Offline
Newbie Poster

Re: still struggling - please help

 
0
  #7
Mar 3rd, 2009
Originally Posted by vinothkumarc View Post
hey... y u copied... and pasted without any comments (my code)
umm... i dont know you, have never heard of you and its not the same code... a friend helped me with it... do you have another user name?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: still struggling - please help

 
0
  #8
Mar 3rd, 2009
I think nav33n (who re-posted your code) was trying to encourage you to use code tags.

Please use code tags when posting code. For more information, click here.

When posting php code, please use php code tags
[code=php]
// Your code here
[/code]
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,746
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 330
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: still struggling - please help

 
0
  #9
Mar 3rd, 2009
Originally Posted by vinothkumarc View Post
hey... y u copied... and pasted without any comments (my code)
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,449
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 135
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: still struggling - please help

 
0
  #10
Mar 3rd, 2009
Originally Posted by vinothkumarc View Post
hey... y u copied... and pasted without any comments (my code)
It appears he didn't really copy your code but instead quoted your post.
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC