Forum Moderators: coopster

Message Too Old, No Replies

php sql search checkbox

         

numbers

1:34 am on Oct 20, 2009 (gmt 0)

10+ Year Member



I am a complete newb to PHP / MYSQL so please go easy on me. I searched online plenty and the most helpful result was [webmasterworld.com...] that thread.

I essentially want to have several columns in mysql with various types of crude oils ranging by date. I want to be able to search by date and select which column (type of crude) to display. Ultimately I want to be able to have a graph of the selected date range with the types of crudes and a math formula such as a 3-2-1 crack spread [en.wikipedia.org...] and an option to export as a CSV.

So first thing first getting it to display the search result.

search.php
[codes]
<div id="Header"><a href="" title="Crude Data">Crude Data</a></div>

<div id="Content">
<h1>Edit / Delete An Entry</h1>
<BR>
<?php

$db = mysql_connect($host, $username, $password);

if (!$db)
{
print "Could not connect to MySQL server!<BR>";
exit;
}

$result = mysql_select_db($database, $db);

if (!$result) { // Did selection fail?
// Handle error
print "DB Selection failed.<br>Error # $mysql_errno()<BR>Error msg: $mysql_e
rror()";
exit;
}
// Make a MySQL Connection

$dsql = "select ddates from crude";
$result1 = mysql_query($dsql) or die(mysql_error());
$result2 = mysql_query($dsql) or die(mysql_error());

?>
<form action="view.php" method="POST">
<table width="50%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2">Date Range: <br /></td>
</tr>
<tr>
<td>Start Date: <br /></td>
<td>Stop Date: <br /></td>
</tr>
<tr>
<td><select name='xdate'>
<option value=''>Select Start Time</option>
<?php
while ($row = mysql_fetch_assoc($result1)) {
echo "<option value='{$row['ddates']}'>{$row['ddates']}</option>";
}
?>
</select></td>
<td><select name='zdate'>
<option value=''>Select Stop Time</option>
<?php
while ($row = mysql_fetch_assoc($result2)) {
echo "<option value='{$row['ddates']}'>{$row['ddates']}</option>";
}
?>
</select></td>
</tr>
</table>
<br />
Select Resource: <br />
<select name="fromwhere">
<option value="crude">EIA</option>
<option value="platts">platts</option>
</select>
<br />
<br />
Select Data: <br />
<input name="view[]" type="checkbox" value="USGC Conventional Regular Gasoline (cents per gallon)">USGC Conventional Regular Gasoline (cents per gallon)</input><br><br>
<input name="view[]" type="checkbox" value="Heating Oil (HSD) (cents per gallon)">Heating Oil (HSD) (cents per gallon)</input><br><br>
<input name="view[]" type="checkbox" value="USGC Low Sulfur Diesel (LSD) (cents per gallon)">USGC Low Sulfur Diesel (LSD) (cents per gallon)</input><br><br>
<input name="view[]" type="checkbox" value="WTI Crude Cushing (WTI) (Dollar per barrel)">WTI Crude Cushing (WTI) (Dollar per barrel)</input><br><br>
<input name="view[]" type="checkbox" value="West Texas Sour (WTS) (Dollar per barrel)">West Texas Sour (WTS) (Dollar per barrel)</input><br><br>
<input name="view[]" type="checkbox" value="Louisiana Sweet (LLS) (Dollar per barrel)">Louisiana Sweet (LLS) (Dollar per barrel)</input>
<br />
<br />
<input type ="submit" value="SUBMIT"/>
</form> [/codes]

view.php

<div id="Header"><a href="" title="Crude Chart">Crude Chart</a></div>
<div id="Content">
<?php

$db = mysql_connect($host, $username, $password);

if (!$db)
{
print "Could not connect to MySQL server!<BR>";
exit;
}

$result = mysql_select_db($database, $db);

if (!$result) { // Did selection fail?
// Handle error
print "DB Selection failed.<br>Error # $mysql_errno()<BR>Error msg: $mysql_error()";
exit;
}
// Make a MySQL Connection

if(isset($_POST['fromwhere'])):
$fromwhere = $_POST['fromwhere'];
$x = DATE($_POST['xdate']);
$z = DATE($_POST['zdate']);
$select = implode(", ", $_POST["view"]);

$result = @mysql_query("SELECT ".$select.", FROM ddates as date_string FROM $fromwhere WHERE ddates>='$x' AND ddates<='$z'");
if (!$result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
?>
<table border="1" align="center" cellpadding="2" cellspacing="2" bordercolor="#000000">
<tr><th>Day</th><th>USGC Conventional Regular Gasoline (cents per gallon)</th><th>Heating Oil (HSD) (cents per gallon)</th> <th>USGC Low Sulfur Diesel (LSD) (cents per gallon)</th><th>WTI Crude Cushing (WTI) (Dollar per barrel)</th><th>West Texas Sour (WTS) (Dollar per barrel)</th><th>Louisiana Sweet (LLS) (Dollar per barrel)</th></tr>

<?php
while ($row = mysql_fetch_array($result)) {
// $id=$row['id'];
$dbdate=$row["ddates"];
echo '<tr><td align=center>' . $dbdate . '</td>';
echo '<td align=center>' . $row['USGC Conventional Regular Gasoline (cents per gallon)'] . '</td>';
echo '<td align=center>' . $row['Heating Oil (HSD) (cents per gallon)'] . '</td>';
echo '<td align=center>' . $row['USGC Low Sulfur Diesel (LSD) (cents per gallon)'] . '</td>';
echo '<td align=center>' . $row['WTI Crude Cushing (WTI) (Dollar per barrel)'] . '</td>';
echo '<td align=center>' . $row['West Texas Sour (WTS) (Dollar per barrel)'] . '</td>';
echo '<td align=center>' . $row['Louisiana Sweet (LLS) (Dollar per barrel)'] . '</td>';
echo "</tr>";
}

?>
</table>

upon executing I get an error on line 82 but I can not proceed. i created a free public hosting account here with a preview of this script. any help or guidance appreciated.

thanks in advance

[edited by: eelixduppy at 1:49 am (utc) on Oct. 20, 2009]
[edit reason] no URLs, please [/edit]

TheMadScientist

7:01 am on Oct 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I don't have a clue where line 82 is and really don't feel like counting, but at a glance, my guess is the error is here:
view[]

I saw a recent thread here re passing info from a form as an array, but it didn't work... I thought it was just the PHP for a while, and usually use 'meaningful names' for variables (IOW I would use view_1 or view_something and loop through them) personally, because I don't like to try and pass arrays through forms, but the other day I was asked to fix a form which was not working correctly... The form suddenly stopped sending the variables passed as arrays to e-mail. I looked at the manual and the form was written correctly according to the book, so, IMO for some reason PHP is not receiving arrays from forms correctly in some 'newer' version.

Try removing the array to be passed from your form and see what happens... I switched the form I was asked to fix to unique variables rather than an array and used a for loop with a conditional (if) and a simple counter to extract the information from the unique variables rather than passing an array.

eeek

11:41 pm on Oct 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



upon executing I get an error on line 82

And we're supposed to guess what that error message says?

numbers

4:20 am on Oct 21, 2009 (gmt 0)

10+ Year Member



sorry, i put a link to the script but I didnt know it was not allowed. the error is
"
Parse error: syntax error, unexpected $end in /crude/view.php on line 82". their is no line 82, it stops at 81.

Just to be clear the search page brings up all the information correctly including the dates. I'll try what you said madscientist and report back.

thanks

TheMadScientist

4:25 am on Oct 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In my experience it means you are missing a } somewhere. It's usually an 'if' for me...

<added>
It's this one:
if(isset($_POST['fromwhere'])):

There's no endif; using the alternate syntax.
</added>