Jump to content

mysql query WHERE var AND var


Winston_Smith

Recommended Posts

I'm trying to print out data into a table if both variables are found in the same row. The problem is that it seems to print everything that matches the first variable ($town) and ignores the second ($lname)

 

<?php
elseif (isset($town, $lname)){
  print <<<_HTML_
<center>
<img src="ecfiber.jpg">

<table width="800" border="1">
  <tr>
    <th scope="col">First Name</th>
    <th scope="col">Last Name</th>
    <th scope="col">E-911Address</th>
    <th scope="col">Town</th>
    <th scope="col">E-Mail</th>
  </tr>
_HTML_;

$result = mysql_query("SELECT * FROM `ecfiber` WHERE `town`='".$town."' AND `lname`='".$lname."'");
while($row = mysql_fetch_array($result))
  {
print "<tr><td>";
  echo $row['fname'];
  print "</td><td>";
  echo $row['lname'];
  print "</td><td>";
  echo $row['address'];
  print "</td><td>";
  echo $row['town'];
  print "</td><td>";
  echo $row['mail'];
  print "</td></tr>";
  }
  
  print "</table>";
  print "<a href = \"index.html\"> Return To Main Index</a><br>";
  print  "<a href = \"listdb.html\">View Database</a></center>";
  }
?>
  

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/120036-mysql-query-where-var-and-var/
Share on other sites

Nevermind, I'm stupid. I have a if statement before this elseif statement that checks if $town is set then prints a table out. Of course it's going to be set so it's printing out that table and ending.

 

I'll mark this as solved, untill I fix that part. Then we'll see if I have the some problem my guess is I won't.

Ok still having problems, now I can't get anything to print at all. I made a few changes so I could search by lname and town, and it stopped working. So I undid everything I changed (that I could remember) and now it won't print anything but the links at the bottom, and they print from the elseif isset section.

 

Here's the code it's kind of long.

 

<?php
//include config
include ("config.php");
//Connect to Database
mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error());
mysql_select_db("$dbname") or die(mysql_error());
//set Variables
$town = $_POST['town'];
$lname = $_POST['lname'];
$full = $_POST['full'];

//Check if box is checked, if true unset other variables
if ( $full == true ) {
	unset ($town);
	unset ($lname);
	} 
//Check if $town and $lname are set, if true set new variables and unset old
if (isset($town, $lname)) {
	$town2 = "$town";
	$lname2 = "$lname";
	unset ($town);
	unset ($lname);
	}
//Print Page title
print <<<_HTML_
<html>
<head><title>ECFiber Pre-Registration</title></head>
<body>
_HTML_;
//Check is $town is set, if set print all rows containing $town
if ( $town == true ) {
print <<<_HTML_
<center>
<img src="ecfiber.jpg">

<table width="800" border="1">
  <tr>
    <th scope="col">First Name</th>
    <th scope="col">Last Name</th>
    <th scope="col">E-911 Address</th>
    <th scope="col">Town</th>
    <th scope="col">E-Mail</th>
  </tr>
_HTML_;

$result = mysql_query("SELECT * FROM `ecfiber`
WHERE `town` ='".$town."'");

while($row = mysql_fetch_array($result)){
print "<tr><td>";
  echo $row['fname'];
  print "</td><td>";
  echo $row['lname'];
  print "</td><td>";
  echo $row['address'];
  print "</td><td>";
  echo $row['town'];
  print "</td><td>";
  echo $row['mail'];
  print "</td></tr>";
  }
  
  print "</table>";
  print "<a href = \"index.html\"> Return To Main Index</a><br>";
  print  "<a href = \"listdb.html\">View Database</a></center>";
//Check if $lname is set, is set print all rows containing $lname
} 
elseif( $lname == true ){
print <<<_HTML_
<center>
<img src="ecfiber.jpg">

<table width="800" border="1">
  <tr>
    <th scope="col">First Name</th>
    <th scope="col">Last Name</th>
    <th scope="col">E-911Address</th>
    <th scope="col">Town</th>
    <th scope="col">E-Mail</th>
  </tr>
_HTML_;

$result = mysql_query("SELECT * FROM `ecfiber`
WHERE `lname` ='".$lname."'");

while($row = mysql_fetch_array($result)){
print "<tr><td>";
  echo $row['fname'];
  print "</td><td>";
  echo $row['lname'];
  print "</td><td>";
  echo $row['address'];
  print "</td><td>";
  echo $row['town'];
  print "</td><td>";
  echo $row['mail'];
  print "</td></tr>";
  }
  
  print "</table>";
  print "<a href = \"index.html\"> Return To Main Index</a><br>";
  print  "<a href = \"listdb.html\">View Database</a></center>";
//Check if $full is set, if true print database 
  } 
  elseif( $full == true ){
print <<<_HTML_
<center>
<img src="ecfiber.jpg">

<table width="800" border="1">
  <tr>
    <th scope="col">First Name</th>
    <th scope="col">Last Name</th>
    <th scope="col">E-911Address</th>
    <th scope="col">Town</th>
    <th scope="col">E-Mail</th>
  </tr>
_HTML_;

$result = mysql_query("SELECT * FROM `ecfiber`") 
or die(mysql_error()); 

while($row = mysql_fetch_array( $result )) {
  
  print "<tr><td>";
  echo $row['fname'];
  print "</td><td>";
  echo $row['lname'];
  print "</td><td>";
  echo $row['address'];
  print "</td><td>";
  echo $row['town'];
  print "</td><td>";
  echo $row['mail'];
  print "</td></tr>";
  }
  
  print "</table>";
  print "<a href = \"index.html\"> Return To Main Index</a><br>";
  print  "<a href = \"listdb.html\">View Database</a></center>";
  } 
//Check if $town2 and $lname2 are set, if set print all rows containing $town2 and $lname2  
elseif (isset($town2, $lname2)){
  print <<<_HTML_
<center>
<img src="ecfiber.jpg">

<table width="800" border="1">
  <tr>
    <th scope="col">First Name</th>
    <th scope="col">Last Name</th>
    <th scope="col">E-911 Address</th>
    <th scope="col">Town</th>
    <th scope="col">E-Mail</th>
  </tr>
_HTML_;

$result = mysql_query("SELECT * FROM `ecfiber` WHERE `town` ='".$town2."' AND `lname` ='".$lname2."'");
while($row = mysql_fetch_array($result)){
print "<tr><td>";
  echo $row['fname'];
  print "</td><td>";
  echo $row['lname'];
  print "</td><td>";
  echo $row['address'];
  print "</td><td>";
  echo $row['town'];
  print "</td><td>";
  echo $row['mail'];
  print "</td></tr>";
  }
  
  print "</table>";
  print "<a href = \"index.html\"> Return To Main Index</a><br>";
  print  "<a href = \"listdb.html\">View Database</a></center>";
  
  }
  
  
  else {
  print "<title>ECFiber Pre-Registration</title>";
  print "<center>Please click back and make a selection";
  print "<a href = \"index.html\"> Return To Main Index</a></center><br>";

  }
  
?>

 

Incase it matters I'm using xampp 1.6.5

Archived

This topic is now archived and is closed to further replies.

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