I getting a error message let me describe you in detail, Actually i tried to export the report in the CSV format so first i display the table on the page and in the bottom of the page i create a button "Export CSV"
when i user click on the button report should be export but i am getting a error after click on this button .
I troubleshoot this error and get to know that my select query which is in line no. 156 unable to get value from $getdata variable i dont know why. i echo my select query and get this warning message
SELECT ID,DATE,JOBNAME,TIME,INITIALS FROM baltimore where DATE='' Warning: Invalid argument supplied for foreach() in /var/www/html/checklist/Project/report.php on line 165
please check my code below and let me know what modification do i need to sort it out.
<?php
date_default_timezone_set('Asia/Calcutta'); //Specify The Default Timezone
$date=date("m/d/Y");
echo "Today Date Is = ";
echo "$date";
$con = mysql_connect("localhost","root","access"); //Established Connection
if(!$con)
{
die("Unable Connect To Database Server " .mysql_error());
}
$selectdb=mysql_select_db("sample",$con); //Assignning Database to selectdb variable
if(!$selectdb)
{
die ("Database Connection Error" .mysql_error());
}
$table="baltimore";
global $getdata;
$getdata = $_POST['search'];
echo "<br>$getdata";
if(isset($_POST['submit']))
{
$selectcondition = "SELECT DATE FROM baltimore where DATE='".$_POST['search']."'"; //Select Query Which check the date whether the checklist for the given date is exit or not.
$check=mysql_query($selectcondition,$con);
$fetch=mysql_fetch_row($check);
if(!$fetch) //If checklist for the given date is not exit execute if condition
{
echo '"<font color="red">';
echo "<br>";
echo "No Checklist Found In The Database";
echo "<br>";
echo "</font>";
}
else //if checklist for the given date is exit into the database execute this else part
{
$sql= "select DATE,JOBNAME,TIME,INITIALS,TIMESTAMP from $table where DATE='".$_POST['search']."'";
$comment= "select COMMENTS,DATE from comments where DATE='".$_POST['search']."'";
$my_comments=mysql_query($comment,$con);
$mydata=mysql_query($sql,$con);
echo "<table width='100%' height='10%' border='1' cellspacing='0'>
<tr>
<th>DATE</th>
<th>JOBNAME</th>
<th>TIME</th>
<th>INITIALS</th>
<th>SYSTEM TIMESTAMP</th>
</tr>";
while($records = mysql_fetch_array($mydata))
{
echo "<tr>";
echo "<td>" . $records['DATE'] . " </td>";
echo "<td>" . $records['JOBNAME']. " </td>";
echo "<td>" . $records['TIME'] . " </td>";
echo "<td>" . $records['INITIALS'] . " </td>";
echo "<td>" . $records['TIMESTAMP'] . " </td></tr>";
}
$records_comments = mysql_fetch_array($my_comments);
echo "<tr>";
echo '<td >';
echo "<h4>COMMENTS</h4>";
echo '</td><td colspan="2" >';
echo $records_comments['COMMENTS'];
echo '</td></tr>';
}
echo "</table>";
}
echo "<html>";
echo "<body>";
echo "<b> Generate Report </b>";
echo "<br>";
echo "<form action='report.php' method='post'>";
echo "<center>";
echo "<input type='submit' name='csv' value='CSV Report'>";
echo "</html> </body> </form>";
if(isset($_POST['csv']))
{
echo "$getdata";
$con = mysql_connect("localhost","root","access"); //Established Connection
if(!$con)
{
die("Unable Connect To Database Server " .mysql_error());
}
$selectdb=mysql_select_db("sample",$con); //Assignning Database to selectdb variable
if(!$selectdb)
{
die ("Database Connection Error" .mysql_error());
}
$table="baltimore";
$filename = 'uploads/'.strtotime("now").'.csv';
$fp=fopen($filename,"w");
echo "$getdata";
echo $selectcondition = "SELECT ID,DATE,JOBNAME,TIME,INITIALS FROM baltimore where DATE='".$getdata."'";
$mydata=mysql_query($selectcondition,$con);
$row=mysql_fetch_assoc($mydata);
$seperator="";
$comma="";
foreach($row as $name => $value)
{
$seperator .= $comma . '' .str_replace('','""',$name);
$comma = ",";
}
$seperator .= "\n";
fputs($fp,$seperator);
mysql_data_seek($mydata,0);
while($row=mysql_fetch_assoc($mydata))
{
$seperator="";
$comma="";
foreach($row as $name => $value)
{
$seperator .= $comma . '' .str_replace('','""',$value);
$comma = ",";
}
$seperator .= "\n";
//echo $seperator;
fputs($fp,$seperator);
}
fclose($fp);
}


