4 Replies - 446 Views - Last Post: 04 March 2009 - 01:02 AM Rate Topic: -----

#1 sharlene_c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 01-February 09

still struggling - please help

Post icon  Posted 02 March 2009 - 07:21 PM

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


Is This A Good Question/Topic? 0
  • +

Replies To: still struggling - please help

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2892
  • View blog
  • Posts: 10,025
  • Joined: 08-August 08

Re: still struggling - please help

Posted 02 March 2009 - 08:34 PM

Where is it failing?

I would start off by commenting out everything below:

$sql = ...

and I'd echo $sql to make sure it's producing the query that I expect. Once that was working, I'd move the start-comment: /* down just below the first if statement and check the results for that. Keep doing that until you get to the bottom of the script and it will have to work!
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: still struggling - please help

Posted 03 March 2009 - 05:34 AM

According to your form data, the value of $gender will be "" if both genders are desired, not "both."
Was This Post Helpful? 0
  • +
  • -

#4 samoi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 12-February 09

Re: still struggling - please help

Posted 04 March 2009 - 12:10 AM

as far as I know,

You probably should have asking for completing the query in every if statement !

so you have :
if(!$gender)
{
$SQL = "SELECT * FROM names";
$result = mysql_query($SQL) or die(mysql_error()); // do error checking every time !
// then print it out!
}
else
$SQL = "SELECT * FROM names WHERE name = '$gender'";
$result = mysql_query($SQL) or die(mysql_error()); // here too !
// then loop and print !




just want to help, sorry if I'm wrong !

This post has been edited by samoi: 04 March 2009 - 12:11 AM

Was This Post Helpful? 0
  • +
  • -

#5 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: still struggling - please help

Posted 04 March 2009 - 01:02 AM

View Postsamoi, on 4 Mar, 2009 - 02:10 AM, said:

as far as I know,

You probably should have asking for completing the query in every if statement !

so you have :
if(!$gender)
{
$SQL = "SELECT * FROM names";
$result = mysql_query($SQL) or die(mysql_error()); // do error checking every time !
// then print it out!
}
else
$SQL = "SELECT * FROM names WHERE name = '$gender'";
$result = mysql_query($SQL) or die(mysql_error()); // here too !
// then loop and print !




just want to help, sorry if I'm wrong !


The way he's doing it is actually more efficient, but thanks for offering :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1