Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

Getting Undefined Index Error:

General discussions related to php

Moderators: macek, egami, gesf

Getting Undefined Index Error:

Postby Gamers » Sun Oct 28, 2012 3:00 am

Notice: Undefined index: search in /var/www/html/checklist/Project/report.php on line 47

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'] . "&nbsp;</td>";
echo "<td>" . $records['JOBNAME']. "&nbsp;</td>";
echo "<td>" . $records['TIME'] . "&nbsp;</td>";
echo "<td>" . $records['INITIALS'] . "&nbsp;</td>";
echo "<td>" . $records['TIMESTAMP'] . "&nbsp;</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);

}

Gamers
New php-forum User
New php-forum User
 
Posts: 48
Joined: Mon Jun 11, 2012 9:13 am

Re: Getting Undefined Index Error:

Postby seandisanti » Fri Nov 02, 2012 2:56 pm

I believe your issue is in your for each $row, as right before you do a for each, you define a $row to be a single row. when it is evaluated in your loop, it's not a member of a collection, it's a row.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 787
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP General

Who is online

Users browsing this forum: Google [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron