mikebyrne Posted March 16, 2009 Share Posted March 16, 2009 At present my code can search my DB for First name but I want to be able to search by First name,Surname or by both How can I modify my code to allow for this? My code at present is <? include("connect.php"); // now you are connected and can query the database if(!empty($_POST['Fname'])) { $Address2 = $_POST['Address2']; $Fname=$_POST['Fname']; $Sname=$_POST['Sname']; $Address1=$_POST['Address1']; $Address2=$_POST['Address2']; $Address3=$_POST['Address3']; $Address4=$_POST['Address4']; $Station=$_POST['Station']; $Vote=$_POST['Vote']; $sql = "Select `Fname`,`Sname`, `Address1`, `Address2`, `Address3`, `Address4` FROM `Athy` WHERE `Fname`='".$Fname."'"; //echo $sql; $var = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($var) > 0) { while ($row = mysql_fetch_assoc($var)) { echo $row['Fname'].' '.$row['Sname'].' '.$row['Address1'].' '.$row['Address2'].' '.$row['Address3'].' '.$row['Address4']. '<br/>'; } } else { echo "Failed"; } } else{ ?> <html> <style type="text/css"> label { width:150px; margin-right:5px; text-align:right; display:block; float:left; } .style1 {font-weight: bold} </style> <body> <form name="test.php" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p><img src="untitled.jpg" alt="" width="994" height="176" /></p> <p align="left"><label for "Firstname"><strong>Firstname</strong></label> <input type="text" name="Fname" value="<?php echo $Fname; ?>"> </p> <p align="left"><label for "Lastname"><strong>Lastname</strong></label> <input type="text" name="Sname" value="<?php echo $Sname; ?>"> </p> <p align="left"> <input name="submit" value="Submit" type="submit" /> </p> </form> </body> </html> <? } // don't forget to close the mysql connection mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/149706-solved-search-db-by-firstname-andor-surname/ Share on other sites More sharing options...
Daniel0 Posted March 17, 2009 Share Posted March 17, 2009 $sql = 'Select `Fname`,`Sname`, `Address1`, `Address2`, `Address3`, `Address4` FROM `Athy` WHERE '; if ($Fname && !$Sname) { $sql .= "`Fname` = '" . mysql_real_escape_string($Fname) . "'"; } else if (!$Fname && $Sname) { $sql .= "`Sname` = '" . mysql_real_escape_string($Sname) . "'"; } else if ($Fname && $Sname) { $sql .= "`Fname` = '" . mysql_real_escape_string($Fname) . "' OR `Sname` = '" . mysql_real_escape_string($Sname) . "'"; } else { throw Exception('Both first name and last name are empty.'); } You need to escape your values or bad things will happen. Quote Link to comment https://forums.phpfreaks.com/topic/149706-solved-search-db-by-firstname-andor-surname/#findComment-786552 Share on other sites More sharing options...
mikebyrne Posted March 17, 2009 Author Share Posted March 17, 2009 Im now getting the following error: Parse error: parse error in C:\xampp\htdocs\Name.php on line 42 Line 42 is else{ My complete code is: <? include("connect.php"); // now you are connected and can query the database if(!empty($_POST['Fname'])) { $Address2 = $_POST['Address2']; $Fname=$_POST['Fname']; $Sname=$_POST['Sname']; $Address1=$_POST['Address1']; $Address2=$_POST['Address2']; $Address3=$_POST['Address3']; $Address4=$_POST['Address4']; $Station=$_POST['Station']; $Vote=$_POST['Vote']; $sql = 'Select `Fname`,`Sname`, `Address1`, `Address2`, `Address3`, `Address4` FROM `Athy` WHERE '; if ($Fname && !$Sname) { $sql .= "`Fname` = '" . mysql_real_escape_string($Fname) . "'"; } else if (!$Fname && $Sname) { $sql .= "`Sname` = '" . mysql_real_escape_string($Sname) . "'"; } else if ($Fname && $Sname) { $sql .= "`Fname` = '" . mysql_real_escape_string($Fname) . "' OR `Sname` = '" . mysql_real_escape_string($Sname) . "'"; } else { throw Exception('Both first name and last name are empty.'); } else{ ?> <html> <style type="text/css"> label { width:150px; margin-right:5px; text-align:right; display:block; float:left; } .style1 {font-weight: bold} </style> <body> <form name="test.php" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p><img src="untitled.jpg" alt="" width="994" height="176" /></p> <p align="left"><label for "Firstname"><strong>Firstname</strong></label> <input type="text" name="Fname" value="<?php echo $Fname; ?>"> </p> <p align="left"><label for "Lastname"><strong>Lastname</strong></label> <input type="text" name="Sname" value="<?php echo $Sname; ?>"> </p> <p align="left"> <input name="submit" value="Submit" type="submit" /> </p> </form> </body> </html> <? } // don't forget to close the mysql connection mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/149706-solved-search-db-by-firstname-andor-surname/#findComment-786557 Share on other sites More sharing options...
Daniel0 Posted March 17, 2009 Share Posted March 17, 2009 Well, ok. You can just remove my else block. Quote Link to comment https://forums.phpfreaks.com/topic/149706-solved-search-db-by-firstname-andor-surname/#findComment-786563 Share on other sites More sharing options...
mikebyrne Posted March 17, 2009 Author Share Posted March 17, 2009 Im now getting the error Fatal error: Call to undefined function exception() in C:\xampp\htdocs\Name.php on line 37 Line 37 = throw Exception('Both first name and last name are empty.'); My code looks like <? include("connect.php"); // now you are connected and can query the database if(!empty($_POST['Fname'])) { $Address2 = $_POST['Address2']; $Fname=$_POST['Fname']; $Sname=$_POST['Sname']; $Address1=$_POST['Address1']; $Address2=$_POST['Address2']; $Address3=$_POST['Address3']; $Address4=$_POST['Address4']; $Station=$_POST['Station']; $Vote=$_POST['Vote']; $sql = 'Select `Fname`,`Sname`, `Address1`, `Address2`, `Address3`, `Address4` FROM `Athy` WHERE '; if ($Fname && !$Sname) { $sql .= "`Fname` = '" . mysql_real_escape_string($Fname) . "'"; } else if (!$Fname && $Sname) { $sql .= "`Sname` = '" . mysql_real_escape_string($Sname) . "'"; } else if ($Fname && $Sname) { $sql .= "`Fname` = '" . mysql_real_escape_string($Fname) . "' OR `Sname` = '" . mysql_real_escape_string($Sname) . "'"; } throw Exception('Both first name and last name are empty.'); } else{ ?> <html> <style type="text/css"> label { width:150px; margin-right:5px; text-align:right; display:block; float:left; } .style1 {font-weight: bold} </style> <body> <form name="test.php" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p><img src="untitled.jpg" alt="" width="994" height="176" /></p> <p align="left"><label for "Firstname"><strong>Firstname</strong></label> <input type="text" name="Fname" value="<?php echo $Fname; ?>"> </p> <p align="left"><label for "Lastname"><strong>Lastname</strong></label> <input type="text" name="Sname" value="<?php echo $Sname; ?>"> </p> <p align="left"> <input name="submit" value="Submit" type="submit" /> </p> </form> </body> </html> <? } // don't forget to close the mysql connection mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/149706-solved-search-db-by-firstname-andor-surname/#findComment-786576 Share on other sites More sharing options...
Daniel0 Posted March 17, 2009 Share Posted March 17, 2009 Right, just remove that entire line. Quote Link to comment https://forums.phpfreaks.com/topic/149706-solved-search-db-by-firstname-andor-surname/#findComment-786587 Share on other sites More sharing options...
mikebyrne Posted March 17, 2009 Author Share Posted March 17, 2009 I've removed the line but now I'm not getting any values returned, just a blank screen <? include("connect.php"); // now you are connected and can query the database if(!empty($_POST['Fname'])) { $Address2 = $_POST['Address2']; $Fname=$_POST['Fname']; $Sname=$_POST['Sname']; $Address1=$_POST['Address1']; $Address2=$_POST['Address2']; $Address3=$_POST['Address3']; $Address4=$_POST['Address4']; $Station=$_POST['Station']; $Vote=$_POST['Vote']; $sql = 'Select `Fname`,`Sname`, `Address1`, `Address2`, `Address3`, `Address4` FROM `Athy` WHERE '; if ($Fname && !$Sname) { $sql .= "`Fname` = '" . mysql_real_escape_string($Fname) . "'"; } else if (!$Fname && $Sname) { $sql .= "`Sname` = '" . mysql_real_escape_string($Sname) . "'"; } else if ($Fname && $Sname) { $sql .= "`Fname` = '" . mysql_real_escape_string($Fname) . "' OR `Sname` = '" . mysql_real_escape_string($Sname) . "'"; } } else{ ?> <html> <style type="text/css"> label { width:150px; margin-right:5px; text-align:right; display:block; float:left; } .style1 {font-weight: bold} </style> <body> <form name="test.php" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p><img src="untitled.jpg" alt="" width="994" height="176" /></p> <p align="left"><label for "Firstname"><strong>Firstname</strong></label> <input type="text" name="Fname" value="<?php echo $Fname; ?>"> </p> <p align="left"><label for "Lastname"><strong>Lastname</strong></label> <input type="text" name="Sname" value="<?php echo $Sname; ?>"> </p> <p align="left"> <input name="submit" value="Submit" type="submit" /> </p> </form> </body> </html> <? } // don't forget to close the mysql connection mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/149706-solved-search-db-by-firstname-andor-surname/#findComment-786609 Share on other sites More sharing options...
Daniel0 Posted March 17, 2009 Share Posted March 17, 2009 You obviously still need to actually execute the query and fetch the rows from the result like you did in your first code snippet. Quote Link to comment https://forums.phpfreaks.com/topic/149706-solved-search-db-by-firstname-andor-surname/#findComment-786618 Share on other sites More sharing options...
mikebyrne Posted March 17, 2009 Author Share Posted March 17, 2009 Thanks for the help! I have it now Quote Link to comment https://forums.phpfreaks.com/topic/149706-solved-search-db-by-firstname-andor-surname/#findComment-786740 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.