根据以前的ID查看表格

时间:2014-05-05 20:34:52

标签: php sql

我有一个名为Modules的表设置,一旦用户登录它就会显示与该用户相关的模块,这样可以正常工作。在表格中每个模块的侧面,我有一个课程链接。一旦用户点击它,我希望它根据模块ID显示课程。

任何帮助都会非常充实。

我的模块代码以防我需要添加内容如下;

<?
include('../inc/security.inc.php');
authorise();

// Include databse connection file
include('../inc/connection.inc.php');

// Connect to the database
connect();

$userID = $_SESSION['userID'];
$sql = "SELECT * FROM tblModule WHERE userID = '$userID'" ;
$result = @mysql_query($sql) or die(mysql_error());

?>

并显示模块表如下;

<?php
// run a while loop through all records and create a new row for each one
while ($record = mysql_fetch_object($result))

{

?>
<table class="myTable">
    <th class="col">Module ID</th>
    <th class="col">Module Title</th>
    <th class="col">Module Description</th>
    <th class="col">User ID</th>
    <th class="col">Manage</th>
</tr>

    <tr class="row">
<td class="cell"><?php echo $record->moduleID; ?></td>
    <td class="cell"><?php echo $record->moduleTitle; ?></td>
    <td class="cell"><?php echo $record->moduleDescription; ?></td>
<td class="cell"><?php echo $record->userID; ?></td>
    <td class="cell"><a href="./lessons.php?moduleID=<? echo $record->moduleID; ?>">Lessons</a></td>
    </tr>

</table>
<?
}
// clean up after ourselves by cleating $result and closing the database connection
mysql_free_result($result);
mysql_close();

?> 

然后到目前为止我在课程表中;

<?
include('../inc/security.inc.php');
authorise();

// Include databse connection file
include('../inc/connection.inc.php');

// Connect to the database
connect();

$moduleID = $_SESSION['moduleID'];
$sql = "SELECT * FROM tblLessons WHERE moduleID = '$moduleID'" ;
$result = @mysql_query($sql) or die(mysql_error());

?>

结果显示为;

<?php
// run a while loop through all records and create a new row for each one
while ($record = mysql_fetch_object($result))
{
?> 
<table class="myTable">
    <th class="col">LessonID</th>
    <th class="col">Lesson Number</th>
    <th class="col">Lesson Description</th>
    <th class="col">ModuleID</th>
    <th class="col">Lesson Plan ID</th>
    <th class="col">Manage</th>
</tr>

<tr class="row">
<td class="cell"><?php echo $record->lessonID; ?></td>
<td class="cell"><?php echo $record->lessonNumber; ?></td>
<td class="cell"><?php echo $record->lessonDescription; ?></td>
<td class="cell"><?php echo $record->moduleID; ?></td>
 <td class="cell"><?php echo $record->lessonPlanID; ?></td>
 </tr>

</table>
<?
}
// clean up after ourselves by cleating $result and closing the database connection
mysql_free_result($result);
mysql_close();

?> 

1 个答案:

答案 0 :(得分:0)

您将moduleID作为查询字符串变量传递给lessons.php,但在Lessions.php中,您需要在会话数据中查找它。

$moduleID = $_SESSION['moduleID'];

尝试:

$moduleID = isset($_GET['moduleID']) ? $_GET['moduleID'] : false;
if ( $moduleID ) {
    $sql = sprintf('SELECT * FROM tblLessons WHERE moduleID = %d', $moduleID);
    $result = @mysql_query($sql) or die(mysql_error());
} else {
    // No moduleID, so show an error message, redirect user, or the like    
}

请注意,我使用sprintf以便$ moduleID转换为数字。您正在使用的代码 - 将值直接传递给MySQL - 是危险的。谷歌:&#34; sql注入&#34;。

如果$ moduleID实际上可以是一个字符串,那么你需要采取额外的步骤来确保数据在传递给MySQL之前被清理,例如。

$moduleID = isset($_GET['moduleID']) ? $_GET['moduleID'] : false;
if ( $moduleID ) {
    $sql = sprintf("SELECT * FROM tblLessons WHERE moduleID = '%s'", mysql_real_escape_string($moduleID));
    $result = @mysql_query($sql) or die(mysql_error());
} else {
    // No moduleID, so show an error message, redirect user, or the like    
}

您还应该考虑切换到PDO,因为mysql_函数已经过折旧。 PDO是一种更容易,更安全的与MySQL交互的方式。