Jump to content

[SOLVED] Search DB by Firstname AND/OR Surname


mikebyrne

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.