Jump to content

Help Filtering Database Results


skot

Recommended Posts

Hi All,

 

I'm stuck with trying to get a to and from date entry form to run a mysql query to show only certain results on the page.

 

Here is my code:

 

<?php

if(!empty($_POST['from'])) $_SESSION['from'] = htmlspecialchars($_POST['from']);
if(!empty($_POST['to'])) $_SESSION['to'] = htmlspecialchars($_POST['to']);

$dbhost = 'localhost';
$dbuser = '**************';
$dbpass = '**************';
$dbname = 'bridgey_centuryfeedback';
$table = 'supportfeedback';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to database');

mysql_select_db($dbname);

@mysql_select_db("$dbname") or die("Error: Unable to select database");

putenv("TZ=Europe/London");

$datetime = date("Y-m-d H:i:s");

// sending query
$result = mysql_query("SELECT * FROM supportfeedback WHERE datetime >= $from AND datetime <= $to");
if (!$result) {
    die("<b>Error: Query failed</b>");
}

if ($to = $from) {
$label = "dated {$to}";
}
elseif ($to <> $from) {
$label = "between {$from} and {$to}";
}

$fields_num = mysql_num_fields($result);

echo "<p style=\"font-family:\"verdana\"; font-size:\"14px\">Customer Feedback Report (Filtered)</p>";
echo "<br><br><p style=\"font-family:\"verdana\"; font-size:\"10px\">Currently showing records {$label}</p>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td style=\"font-family:\"verdana\"; font-size:\"10px\">{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td style=\"font-family:\"verdana\"; font-size:\"10px\">$cell</td>";

    echo "</tr>\n";
}
mysql_free_result($result);

mysql_close($conn);

exit;

?>

 

Are there any obvious errors you can see? Thanks.

Link to comment
https://forums.phpfreaks.com/topic/171549-help-filtering-database-results/
Share on other sites

Oh I also tried this to add the time on as I think that is how it is being stored in the database. And try to see what was being shown in the fields to see if it was working, but that gave me other issues:

 

<?php

if(!empty($_POST['from'])) $_SESSION['from'] = htmlspecialchars($_POST['from']);
if(!empty($_POST['to'])) $_SESSION['to'] = htmlspecialchars($_POST['to']);

$dbhost = 'localhost';
$dbuser = '**************';
$dbpass = '**************';
$dbname = 'bridgey_centuryfeedback';
$table = 'supportfeedback';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to database');

mysql_select_db($dbname);

@mysql_select_db("$dbname") or die("Error: Unable to select database");

putenv("TZ=Europe/London");

$datetime = date("Y-m-d H:i:s");

$from2 = ($from) + " 00:00:00";
$to2 = ($to) + " 23:59:59";

echo ($from2);
echo ($to2);

$querydisplay = ("SELECT * FROM supportfeedback WHERE datetime >= $from2 AND datetime <= $to2")

// sending query
$result = mysql_query("SELECT * FROM supportfeedback WHERE datetime >= $from AND datetime <= $to");
if (!$result) {
    die("<b>Error: Query failed</b><br><br><b>Query ran was:</b> {$querydisplay}");
}

if ($to = $from) {
$label = "dated {$to}";
}
elseif ($to <> $from) {
$label = "between {$from} and {$to}";
}

$fields_num = mysql_num_fields($result);

echo "<p style=\"font-family:\"verdana\"; font-size:\"14px\">Century Software Customer Feedback Report (Filtered)</p>";
echo "<br><br><p style=\"font-family:\"verdana\"; font-size:\"10px\">Currently showing records {$label}</p>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td style=\"font-family:\"verdana\"; font-size:\"10px\">{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td style=\"font-family:\"verdana\"; font-size:\"10px\">$cell</td>";

    echo "</tr>\n";
}
mysql_free_result($result);

mysql_close($conn);

exit;

?>

 

<?php
// sending query
$result = mysql_query("SELECT * FROM supportfeedback WHERE datetime >= $from AND datetime <= $to");
if (!$result) {
    die("<b>Error: Query failed</b>");
}

if ($to = $from) {
$label = "dated {$to}";
}
elseif ($to <> $from) {
$label = "between {$from} and {$to}";
}
?>

 

In the code above. You haven't defined $to and $from. Now unless you have register_globals turned on, this won't work. Try defining those variables and your problem should be solved.

 

Also you must use == when comparing two variables. Using = is for defining variables, not comparing.

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.