我从DB检索数据并插入到html表中但是我想让表中的每个值成为另一个页面的超链接。下面我尝试制作pupil_id并链接到profile.php,但所有pupil_id值现在已经消失了!
(if (!isset($_POST['search'])) {
$pupils = mysql_query("SELECT * FROM pupil") or die("Cant find Pupils");
$count = mysql_num_rows($pupils);
if ($count == 0) {
$totalpupil = "There are currently no Pupils in the system.";
} else {
while ($row = mysql_fetch_array($pupils)) {
?>
<tr>
<td><?php echo '<a href="profile.php?id=' .$row['pupil_id'] . '"</a>' ?></td>
<td><?php echo $row['pupil_name'] ?></td>
<td><?php echo $row['class_id'] ?></td>
</tr>
<?php
}
}
})
整理表应将每个超链接显示为指向另一个页面的超链接。有什么帮助吗?
答案 0 :(得分:3)
由于您的HTML无效,因此您缺少结束>
并且没有为超链接定义文本
<?php echo '<a href="profile.php?id=' .$row['pupil_id'] . '"</a>' ?> //Wrong
正确
<?php echo '<a href="profile.php?id='.$row['pupil_id'].'">'.$row['pupil_id'].'</a>'; ?>
答案 1 :(得分:1)
尝试替换此:
<?php echo '<a href="profile.php?id=' .$row['pupil_id'] . '"</a>' ?>
用这个:
<?php echo "<a href='profile.php?id=".$row['pupil_id']."'>link</a>"; ?>
此外,您根本没有<table>
个标签。
答案 2 :(得分:0)
您不要在链接标记之间添加任何文字<a href="">text here</a>
也许这会对你有所帮助:
<td><?php echo '<a href="profile.php?id=' .$row['pupil_id'] . '">'.$row['pupil_name'].'</a>' ?></td>
答案 3 :(得分:0)
http://uk3.php.net/mysql_query
请注意,您正在学习的资源可能很老。 mysql_query现已弃用。
http://uk3.php.net/manual/en/ref.pdo-mysql.php是替代品。
这是我在不久前写的使用PDO(这更加安全)的启动。
包含此脚本需要访问您的数据库的文件。一个示例文件名是&#39; database.php&#39;但那是你的电话。从&#39; yourproject&#39;设置名称空间。无论你的项目是什么叫做什么。更正数据库凭据以适合您的数据库
这有望为您节省很多麻烦!
我已经在底部给出了一些示例用法。我记得当我开始得到明确的建议有时很难得到。
//***** in a database class file*****/
namespace yourproject;
class Database {
private $db_con = '';
/*** Function to login to the database ***/
public function db_login()
{
// Try to connect
try{
// YOUR LOGIN DETAILS:
$db_hostname = 'localhost';
$db_database = 'yourdatabasename';
$db_username = 'yourdatabaseusername';
$db_password = 'yourdatabasepassword';
// Connect to the server and select database
$this->db_con = new \PDO("mysql:host=$db_hostname;dbname=$db_database",
"$db_username",
"$db_password",
array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
// Prevent emulation of prepared statements for security
$this->db_con->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
$this->db_con->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
return true;
}
// If it fails, send user to maintenance page
catch(PDOException $e)
{
header("location:http://yourwebsiteurl.com/maintenance.php");
exit();
}
}
/*** Function for database control ***/
public function db_control($query , $parameters, $returnID = false)
{
if(!is_array($query) && is_array($parameters))
{
try{
//prepare the statement
$statement = $this->db_con->prepare($query);
//execute the statement
$statement->execute($parameters);
//check whether this is a select, if it is then we need to retrieve the selected data
if(strpos($query, 'SELECT') !== false)
{
//fetch the results
$result = array();
while( $row = $statement->fetch(\PDO::FETCH_ASSOC) )
{
$result[] = $row;
}
//count the results
$count = count($result);
//return the array
return array( 'results' => $result, 'result_count' => $count );
}
//else return the number of affected rows
else{
//count the affected rows and place into a returnable array
$affected_rows = $statement->rowCount();
$returnArray = array('result_count' => $affected_rows);
//check to see if we are to return a newly inserted autoincrement ID from an INSERT
if($returnID)
{
//find the newly created ID and add this data to the return array
$insertID = $this->db_con->lastInsertId();
$returnArray['ID'] = $insertID;
}
return $returnArray;
}
}
catch(PDOException $e)
{
return false;
}
}
else{
return false;
}
}
}
// Start the database class and connect to the database then create a globally accessible function for ease of reference
$db = new \yourproject\Database();
$db->db_login();
function _db( $sql , $params , $returnID = false ){
return $GLOBALS['db']->db_control( $sql , $params , $returnID );
}
当您包含此文件时,您现在有了一个新功能:_db()
。由于函数是全局函数,因此可以从任何类或std文件中调用它。当调用变量时,如下所示将生成如下数组:
array(
'result_count' => 3,
'results' => array(
array(/*row 1*/),
array(/*row 2*/),
array(/*row 3*/),
.. etc etc
)
)
现在在php脚本中包含您的数据库文件:
//call in the database file
require_once 'database.php';
//your query as in the op
$sql = 'SELECT * FROM pupil';
//your params for the query
$params = array();
//running the query and getting the results returned into a variable called $query
$query = _db($sql,$params);
//if no results
if( $query['result_count'] == 0 )
{
echo 'sorry no pupils in the system';
}
else
{
//looping through each result and printing into a html table row
for( $i = 0 ; $i < $query['result_count'] ; ++$i )
{
echo '<tr><td><a href="profile.php?id=' . $query['results'][$i]['pupil_id'] . '"</a></td>';
echo '<td>'. $query['results'][$i]['pupil_name'] . '</td>';
echo '<td>'. $query['results'][$i]['class_id'] . '</td></tr>';
}
}
您的原始查询,但传递了一些参数
//Passing parameters to the query
//your query
$sql = 'SELECT * FROM pupil WHERE pupil_id = :pupil_id AND class_id = :class_id';
//your params for the query
$params = array(
':pupil_id' => 12,
':class_id' => 17,
);
//running the query and getting the results returned into a variable called $query
$query = _db($sql,$params);
//deal with the results as normal...
如果您将第3个参数设置为true,则可以返回刚输入的行的自动ID,例如:
//where $sql is a query that will INSERT a row
$query = _db($sql,$params, true);