MySQL server version 4.1.14
PHP version 4.3.11
phpMyAdmin 2.11.9.6
I’ve been helped so much already by reading other’s posts and answers to a few of my own questions on this forum. I want to start by saying thank you!
I’ve come a long way and have things working as intended…almost!
The scenario:
A client logs in. If successful, the client is sent to a page that lists their projects. The projects are listed with a hyperlink. The hyperlink contains two variables: client name and project_id
<a href="[www.website.com]' . $row['project_id'] .'&client=' .$u .'"> ' .$row['project_name'] . ' </a>
project_id is obtained from the database query and $u is the username obtained from the session variable set at login. After clicking the hyperlink, the client is taken to a page that lists the documents associated with that project.
It all works, which was a big celebration! However… I noticed that if I change the project_id number in the hyperlink, the page updates with the documents pertaining to that project, even if they don’t belong to that client! Yikes! But, if I change the client name, nothing happens. Is that because the client name was pulled from the session variable?
What I need is a way to limit each client to only viewing those projects and documents that belong to them.
I thought maybe I could set a session variable when the user clicks on the hyperlink (thereby choosing a project), but am not sure how to pull that off!
I would really appreciate any nudges in the right direction I could get. It was disappointing to think I was almost finished, only to discover this. But then, better now than when live!
Here’s my code for the projects page where the client is presented with a list of projects.
<?php
session_start(); //start the session
if (!isset($_SESSION['username'] ))
{ header('location: index.php');
} else
{ $section="client-center";
include("../includes/client-section-header.inc.php");
include("../includes/section-navigation.inc.php");
include("../includes/client-pagetitle.inc.php");
$uploads_dir = 'http://www.peerengineering.com/admin/uploads/';
$u = ($_SESSION['username']);
$page_title = "$u";
require_once ('mysql_connect.php'); // Connect to the db.
//get client name
$q = 'SELECT client_name, username FROM clients '; // Make the query
$r = @mysql_query ($q, $link); // Run the query.
if ($r) // ran OK,
{ while ($row = mysql_fetch_array($r, MYSQL_ASSOC))
{ if ($u==$row['username'])
{$cn = $row['client_name'];}
}
mysql_free_result ($r); // Free up the resources.
} // End of if ($r) - get client name
}
echo '<div id="content">
<div id="subleft">';
echo "<h1>$cn</h1>"; // Page header
// Make the query:
$q = 'SELECT project_name, username, project_id FROM projects LEFT JOIN clients ON projects.client_id = clients.client_id ORDER BY username ASC, project_name ASC';
$r = @mysql_query ($q, $link); // Run the query.
if (!r) //could not run query
{echo "could not successfully run query ($r) from database: " . mysql_error();
}
elseif (mysql_num_rows($r) == 0) //no data
{echo "There are no documents in the database.";
}
elseif ($r) // ran OK,
{
echo '<table summary="A listing of the client\\'s documents">
<thead><tr>
<th scope="col" class="col_title">Project</th>
</tr></thead>';
while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
$bg = ($bg=='#dodcbc' ? '#eff3e9' : '#dodcbc'); //switch the bg color
if ($u==$row['username'])
{ echo '<tr bgcolor="' .$bg . '"><td><a href="[www.website.com]' . $row['project_id'] .'&client=' .$u .'"> ' .$row['project_name'] . ' </a></td></tr>';
}
}
echo '</table>'; // Close the table.
mysql_free_result ($r); // Free up the resources.
}
else { echo '<p class="error">The current users could not be retrieved. We apologize for any inconvenience.</p>';// Public message
echo '<p>' . mysql_error($link) . '<br />Query: ' . $q . '</p>';// Debugging message
} // End of if ($r) IF.
mysql_close($link); // Close the database connection.
?>
</div>
<div id="subright">
<div id="sidebar">
<p>If you have any questions about your project, please don’t hesitate to contact your project manager. We’re here to help!</p>
</div>
</div>
</div>
<?php
include("../includes/footer.inc.php");
?>
</div>
</body>
</html>
And the code for the subsequent page that lists the documents associated with that project.
<?php
session_start(); //start the session
if (!isset($_SESSION['username'] ))
{ header('location: index.php');
} else
{ $section="client-center";
$page_title = 'Client Page';
include("../includes/client-section-header.inc.php");
include("../includes/section-navigation.inc.php");
include("../includes/client-pagetitle.inc.php");
$pid = (int)$_REQUEST['proj'];
$u = ($_SESSION['username']);
$client = $_GET['client'];
require_once ('mysql_connect.php'); // Connect to the db.
//get client name
$q = 'SELECT client_name, username FROM clients '; // Make the query
$r = @mysql_query ($q, $link); // Run the query.
if ($r) // ran OK,
{ while ($row = mysql_fetch_array($r, MYSQL_ASSOC))
{ if ($u==$row['username'])
{$cn = $row['client_name'];}
}
mysql_free_result ($r); // Free up the resources.
} // End of if ($r) IF get client name.
//get project name
$q = 'SELECT project_name, project_id FROM projects'; // Make the query
$r = @mysql_query ($q, $link); // Run the query.
if ($r) // ran OK,
{ while ($row = mysql_fetch_array($r, MYSQL_BOTH))
{ if ($pid==(int)$row['project_id'])
{$pn = $row['project_name'];}
}
$proj=(int)$pid;
mysql_free_result ($r); // Free up the resources.
} // End of if ($r) IF get project name.
} //end of if(isset($_SESSION[
?>
<div id="content">
<div id="content-fullwidth">
<?php
echo "<h1>$cn</h1>"; // Page header
?>
<?php
//require_once ('mysql_connect.php'); // Connect to the db.
$first=TRUE;
$q = "SELECT project_name, projects.project_id, document_name, document_type, document_size, date_uploaded, filename FROM documents LEFT JOIN projects ON documents.project_id = projects.project_id WHERE documents.project_id = '$proj' ORDER BY project_name ASC, document_name ASC, date_last_modified DESC";
$r = @mysql_query ($q, $link); // Run the query.
while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
if ($first) {
echo "<h2>Project Name: $pn</h2>";
echo '<table summary="A listing of the project documents">
<thead><tr>
<th scope="col" class="col_title">Title</th>
<th scope="col" class="col_type">Type</th>
<th scope="col" class="col_size">Size (KB)</th>
<th scope="col" class="col_date">Date Uploaded</th>
</tr></thead>';
$first=FALSE;
}//end of $first IF
$bg = ($bg=='#dodcbc' ? '#eff3e9' : '#dodcbc'); //switch the bg color
echo '<tr bgcolor="' .$bg . '">
<td><a href="[www.website.com]' . $row['filename'] .'"> ' . $row['document_name'] . ' </a></td>
<td>' . $row['document_type'] . '</td>
<td>' . $row['document_size'] . '</td>
<td>' . $row['date_uploaded'] . '</td>
</tr>';
} // end of WHILE loop
if ($first) {
echo '<div align="center">There are no documents for this project.</div>';}
else {
echo '</table>
<p>Right-click to download the file or click to view in your browser.</p>';}
mysql_free_result ($r); // Free up the resources.
mysql_close($link); // Close the database connection.
echo '</div></div>';
include("../includes/footer.inc.php");
?>
</div>
</body>
</html>
Again, thanks in advance for taking your time to help me. I really appreciate it.