| | |
still struggling - please help
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Jan 2009
Posts: 13
Reputation:
Solved Threads: 0
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
my form is:
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)
<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:
PHP Syntax (Toggle Plain Text)
<!--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>
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=
As you can see there is no value assigned to that field and that is why it will appear as empty in the database.
html Syntax (Toggle Plain Text)
<input type = "radio" name = "gender" value = ""/>Both
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
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
•
•
Join Date: Apr 2008
Posts: 28
Reputation:
Solved Threads: 3
php Syntax (Toggle Plain Text)
<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>
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.
•
•
•
•
<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>
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Apr 2008
Posts: 28
Reputation:
Solved Threads: 3
hey... y u copied... and pasted without any comments (my code)
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
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]
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]
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
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
![]() |
Similar Threads
- Beginner struggling with what may be obvious, Please help? (C)
- C++ beginner, struggling with parameters (C++)
- VB.NET Threading or Backgroundworker ?? Struggling to get them to work (VB.NET)
- RedHat 8 (*nix Software)
- Struggling with C++ (C++)
- struggling with connecting database to server (ASP.NET)
- Unrecognizable modem (PCI and Add-In Cards)
- No Posts? (DaniWeb Community Feedback)
- PLEASE, I need help with this....why can't I get it to work? (Visual Basic 4 / 5 / 6)
Other Threads in the PHP Forum
- Previous Thread: chown problem
- Next Thread: MYSQL Deleting all topics?
| Thread Tools | Search this Thread |
.htaccess access alexa apache api array beginner binary broken cakephp checkbox class cms code convert cron curl database date directory display dropdown dynamic echo email encode error fairness file files folder form forms function functions google hack href htaccess html htmlspecialchars image include indentedsubcategory insert ip javascript joomla limit link login mail mail() menu methods mlm multiple multipletables mysql network newsletters object oop passwords paypal pdf php problem provider query radio random recursion redirect remote script search secure securephp server sessions simple sms source space sql syntax system table tutorial update upload url user validator variable video voteup web youtube






