?
Solved

Need help reading a PHP Array

Posted on 2009-02-20
4
Medium Priority
?
169 Views
Last Modified: 2012-05-06
I have some unexplained updates going on im my database so I wrote this report code to track what's happening. I am building an array to log into my database but I only want it to log if there is an update. In the places where there is an update I have place the following code:

if ($viewreport) $report['action'] = ("edit");

When I try to write to database only if the action is in the array

if(($viewreport) AND ($report['action'])) {
}
I get this error
"Notice: Undefined index: action in /var/www/html/account.php on line 323"

Thanks, Jeff
<?php
 
function show_report($report) {
	echo "<ul class=\"report\">\n";
	foreach($report as $r) {
		echo "<li>" . $r . "</li>\n";
	}
	echo "</ul>\n";
}
 
$viewreport = 1;
if ($viewreport) $report = array();
if ($viewreport) $report["page"] = ("membreport.php"); 
if ($viewreport) $report['referrer'] = $_SERVER['HTTP_REFERER'];
 
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'main';
 
 
if ($action == "addNow") {
if ($viewreport) $report['action'] = ("addNow");
 
$sql = "Querry goes here";
if ($viewreport) $report["query"] = ("$sql");
 
}
 
 
if ($action == "main") {
 
Contenent Here!
}
 
if($viewreport) { 
// Show report on Page
	echo "Referrer = " . $report['referrer'] . "<br>";
	echo "Page = " . $report['page'] . "<br>";
	echo "Action = " . $report['action'] . "<br>";
	show_report($report); 
}
 
if($viewreport) { 	
// Insert Report in DB
	mysql_query("INSERT INTO tbltrkreport (referrer,page,action,query,other)
				VALUES ('" . $report['referrer'] . "','" . $report['page'] . "','" . $report['action'] . "','" . show_report($report) . "','')") or die ('Database error: MySQL Said : '. mysql_error());
}
?>

Open in new window

0
Comment
Question by:Jeff
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
marchent earned 1600 total points
ID: 23694966
use isset($report['action']) to make sure that the value exist. i.e.

if(isset($report['action'])){
    if(($viewreport) AND ($report['action'])) {
.....
}
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 total points
ID: 23695706
A "Notice" is not an error - it is just a notice.  You can set error_reporting(E_ALL ^ E_NOTICE) and PHP will stop bothering you about the undefined variables.

But you might want to stop using $_REQUEST in this script.  Instead use $_POST.  There is never any circumstance when you would want to update your data base on the basis of something that arrived in the $_GET string!

Cheers, ~Ray
0
 

Author Comment

by:Jeff
ID: 23695765
Good point Ray, Thanks!
0
 

Author Closing Comment

by:Jeff
ID: 31549369
Thanks!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question