我有一个名为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();
?> 发布于 2014-05-05 20:50:39
您将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 --是危险的。谷歌:"sql注入“。
如果$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交互的方法。
https://stackoverflow.com/questions/23481356
复制相似问题