skot Posted August 23, 2009 Share Posted August 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/171549-help-filtering-database-results/ Share on other sites More sharing options...
skot Posted August 23, 2009 Author Share Posted August 23, 2009 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/171549-help-filtering-database-results/#findComment-904672 Share on other sites More sharing options...
skot Posted August 24, 2009 Author Share Posted August 24, 2009 Sorry - bumping - as original post not seen due to the stupid time i posted it Quote Link to comment https://forums.phpfreaks.com/topic/171549-help-filtering-database-results/#findComment-905165 Share on other sites More sharing options...
DJTim666 Posted August 24, 2009 Share Posted August 24, 2009 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/171549-help-filtering-database-results/#findComment-905172 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.