943,816 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 948
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 2nd, 2009
0

still struggling - please help

Expand Post »
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

PHP Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  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.  
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
phpNewbie is offline Offline
13 posts
since Jan 2009
Mar 2nd, 2009
0

Re: still struggling - please help

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'.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Mar 3rd, 2009
0

Re: still struggling - please help

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=
html Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Mar 3rd, 2009
1

Re: still struggling - please help

php Syntax (Toggle Plain Text)
  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.
Reputation Points: 19
Solved Threads: 3
Light Poster
vinothkumarc is offline Offline
28 posts
since Apr 2008
Mar 3rd, 2009
0

Re: still struggling - please help

<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.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Mar 3rd, 2009
0

Re: still struggling - please help

hey... y u copied... and pasted without any comments (my code)
Reputation Points: 19
Solved Threads: 3
Light Poster
vinothkumarc is offline Offline
28 posts
since Apr 2008
Mar 3rd, 2009
0

Re: still struggling - please help

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
phpNewbie is offline Offline
13 posts
since Jan 2009
Mar 3rd, 2009
0

Re: still struggling - please help

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]
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Mar 3rd, 2009
0

Re: still struggling - please help

hey... y u copied... and pasted without any comments (my code)
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Mar 3rd, 2009
0

Re: still struggling - please help

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.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: chown problem
Next Thread in PHP Forum Timeline: MYSQL Deleting all topics?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC