954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

still struggling - please help

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

<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']);


  
$sql = "SELECT * FROM names WHERE gender = '" . $gender . "' and name LIKE '" . $name . "%' and 
meaning LIKE '%" . $meaning . "%' and origin = '" . $origin . "'";
  

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>


my form is:

<!--html page-->
<html>
	<head>
		<title>Baby Names Database</title>
	</head>
	<body>
		<h1>Baby Names</h1>
		<h3>Please fill in the blanks below, and We'll return baby names for you to browse.</h3>

		
			<table border = "1" align = "center">
				<form method = "post" action = "babyNames1.php">
				<tr bgcolor="#cae1ff">
					<td align = "left">
						Gender:
					</td>
					<td>
						<input type = "radio" name = "gender" value = "male"/>Boys Names
						<input type = "radio" name = "gender" value = "female"/>Girls Names
						<input type = "radio" name = "gender" value = ""/>Both
					</td>
				</tr>
				<tr bgcolor="#ffe1ff">
					<td align = "left">
						Meaning:
					</td>
					<td>
						<input type = "text" name = "meaning" value = ""/>
					</td>
				</tr>
				<tr bgcolor="#ffe1ff">
					<td align = "left">
						Name Begins With:
					</td>
					<td>
						<input type = "text" name = "name" value = ""/>
					</td>
				</tr>
				<tr bgcolor="#cae1ff">
					<td align = "left">
						Origin
					</td>
					<td>
					
						<select name = "origin" id = "origin">
							<option value = "any">Any</option>
							<option value = "african">African</option>
							<option value = "anglo">Anglo</option>
							<option value = "arabian">Arabian</option>
							<option value = "arabic">Arabic</option>
							<option value = "aramaic">Aramaic</option>
							<option value = "armenian">Armenian</option>
							<option value = "arthurian">Arthurian</option>
							<option value = "basque">Basque</option>
							<option value = "celtic">Celtic</option>
							<option value = "chamoru">Chamoru</option>
						</select> 
					</td>
				</tr>
			
			</table><p /><center>
<input type = "submit" value = "Show me Names"/>
</center>	
</form>
</body>
</html>
phpNewbie
Newbie Poster
13 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

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'.

Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
 

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=

<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.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 
<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>
vinothkumarc
Light Poster
28 posts since Apr 2008
Reputation Points: 19
Solved Threads: 3
 


<?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());

?>
Name
Gender
Origin
Meaning


<?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);
?>
<?php echo $row['name'];?>
<?php echo $row['gender'];?>
<?php echo $row['origin'];?>
<?php echo $row['meaning'];?>

<?php
}

?>

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

hey... y u copied... and pasted without any comments (my code)

vinothkumarc
Light Poster
28 posts since Apr 2008
Reputation Points: 19
Solved Threads: 3
 
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?

phpNewbie
Newbie Poster
13 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

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]

Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
 
hey... y u copied... and pasted without any comments (my code)

:icon_rolleyes:

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 
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.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

sorry people - i thought i did use the tags but anyway thank you all for your replies - will play with it tomorrow

phpNewbie
Newbie Poster
13 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 
sorry people - i thought i did use the tags but anyway thank you all for your replies - will play with it tomorrow

@phpNewbie, We aren't talking about you. Vinothkumarc didn't use[code] tags. Infact, I appreciate you for using code tags. :)

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Hello Newbie. You don't need to do so much. Really PHP script is simple to write. all you need is make your code like a lawyer builds a case. Let's see if I can use your code to explain somethings to you but right away, I have an issue with a script too. See you in about 10 hours time.
Feel cool, PHP is friendly.

emiola
Junior Poster in Training
51 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
 
Hello Newbie. You don't need to do so much. Really PHP script is simple to write. all you need is make your code like a lawyer builds a case. Let's see if I can use your code to explain somethings to you but right away, I have an issue with a script too. See you in about 10 hours time. Feel cool, PHP is friendly.


hey thanks... i look forward to this too

phpNewbie
Newbie Poster
13 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

Hey thanks so much vinothkumarc! You're awesome... sorry it took me so long to get back and thank you for solving.

phpNewbie
Newbie Poster
13 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You