Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reporting data from database with selection criteria from form

Status
Not open for further replies.

danno74

IS-IT--Management
Nov 13, 2002
295
US
Greetings all,

I have a mysql database that right now I am just spitting all the data from it's only table back to a web page to report it. It contains user data from a software download page for our campus IT department. I thought about making a page that would allow the user to have some selection criteria they could define.

Here is the form:

Code:
<html> 
<h2>Software Downloads Report Page</h2>
<form action="reporttest.php" method="post">
<h3>Search for options where:</h3>

<p>Software equals
<select name="softwaretitle">
  <option value="testfile">Test</option>
  <option value="all">All Software</option>
  </select>
  
<p>Affiliation equals
<select name="affiliation">
	<option value="student">Student</option>
	<option value="staff">Fact/Staff</option>
	<option value="affall">Both Groups</option>
	</select>
  <p>
  Sort by:
<select name="sort">
  <option value="login">Login</option>
  <option value="software_title">Software Title</option>
  <option value="affiliation">Affiliation</option>
  <option value="date_dload">Date</option>
</select>
<p>
<input name="Create Report" type="submit" value="Create Report">
</form>
</html>

And here is the follow up page with the SELECT statements:
Code:
<?php
// Make connection
include("inc/connect.php");

// Take information from form and get data

$software = $_POST['softwaretitle'];
$affiliation = $_POST['affiliation'];
$sort = $_POST['sort'];

if ($affiliation != 'affall' || $software != 'all')
{
$where = "WHERE";
}

if $software != "all"
{
$wheresoft = "software_title = $software";
}

if $affiliation != "affall"
{
$whereaff = "affiliation = $affiliation";
}

if ($affiliation != "affall") && ($software != "all")
{
$or = "or";
}


$query = (SELECT * FROM downloads '$where' '$wheresoft' '$or' '$whereaff' ORDER BY '$sort');

$result = mysql_query( $query )
or die(mysql_error());

echo "<table border='4'>";
echo "<tr> <th>Login</th> <th>Software Title</th> <th>Affiliation</th> <th>Date</th> <th>Time</th> </tr>";

// keeps getting the next row until there are none left

while ($row = mysql_fetch_array( $result )) {
			// print out the contents of each row into a table
			echo "<tr><td>";
			echo $row['login'];
			echo "</td><td>";
			echo $row['software_title'];
			echo "</td><td>";
			echo $row['affiliation'];
			echo "</td><td>";
			echo $row['date_dload'];
			echo "</td><td>";
			echo $row['time'];
			echo "</td></tr>";
			
}

echo "</table>";
?>

Couple questions...

1. I get an error in the apache log:
[error] PHP Parse error: syntax error, unexpected T_BOOLEAN_OR in reporttest.php on line 11

I am trying to use an OR statement that says if either field has something besides include all, that a WHERE will be put into the query. I have tried | and || and get the same error. Any thoughts?

2. In general, is there a better way to do this? I just went through this in my mind and did the code, and I don't have a ton of PHP experience.

Thanks for your time.

- Dan
 
If statements in PHP must be surrounded by parenthesis:
( ... )

Just surround all your statements with parenthesis and you should be fine.

Additionally I suggest you clean your Posted variables before putting them into your query as that can lead to SQL injection :

HAVE A READ:




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks Phil.

The first question I had that included the error was about this statement:

Code:
if ($affiliation != 'affall' || $software != 'all')
{
$where = "WHERE";
}

Why is this giving me an error?

Thanks again!!! And thanks for the injection information.

- Dan
 
Sorry, I meant to say I tried this but it didn't work... always seems to be syntax...

Code:
if (($affiliation != 'affall') || ($software != 'all'))
{
$where = "WHERE";
}

Thanks!

- Dan
 
There's no reason that line would be generating that error, unless your actual code differs from the one posted above.

such as looking like:

Code:
if ($affiliation != 'affall'[red])[/red] || [red]([/red]$software != 'all')

This would indeed cause the error.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Weird... that is the same code. Thanks for the prompt reply.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top