Php 如何在表超链接中生成回显结果

Php 如何在表超链接中生成回显结果,php,sql,hyperlink,html-table,echo,Php,Sql,Hyperlink,Html Table,Echo,我已经从DB中检索到数据并插入到html表中,但是我想使表中的每个值成为指向另一个页面的超链接。下面我尝试了创建瞳孔id并链接到profile.php,但是所有瞳孔id值现在都消失了 (if (!isset($_POST['search'])) { $pupils = mysql_query("SELECT * FROM pupil") or die("Cant find Pupils"); $cou

我已经从DB中检索到数据并插入到html表中,但是我想使表中的每个值成为指向另一个页面的超链接。下面我尝试了创建瞳孔id并链接到profile.php,但是所有瞳孔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
                        }
                    }
                })

整理表应将每个超链接显示为指向另一页的超链接。有什么帮助吗?

因为您的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>'; ?>

因为您的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>'; ?>
尝试替换此:

<?php echo '<a href="profile.php?id=' .$row['pupil_id'] . '"</a>' ?>
为此:

<?php echo "<a href='profile.php?id=".$row['pupil_id']."'>link</a>"; ?>
此外,您根本没有标签。

尝试替换此标签:

<?php echo '<a href="profile.php?id=' .$row['pupil_id'] . '"</a>' ?>
为此:

<?php echo "<a href='profile.php?id=".$row['pupil_id']."'>link</a>"; ?>

此外,您根本没有标签。

您没有在链接标签之间放置任何文本

也许这会帮助你:

 <td><?php echo '<a href="profile.php?id=' .$row['pupil_id']  . '">'.$row['pupil_name'].'</a>' ?></td>

您不会在链接标签之间放置任何文本

也许这会帮助你:

 <td><?php echo '<a href="profile.php?id=' .$row['pupil_id']  . '">'.$row['pupil_name'].'</a>' ?></td>
注意,你正在学习的任何资源都可能非常陈旧。mysql\u查询现在已不推荐使用

这是一个替代品

这是一个使用PDO的启动程序,这比我之前写的安全多了

包含此文件,php脚本需要在其中访问数据库。示例文件名为“database.php”,但这是您的调用。将名称空间从“yourproject”设置为调用项目的名称。更正数据库凭据以适合您的数据库

希望这能帮你省去很多麻烦

我在底部给你举了一些例子。我记得当我刚开始时,得到明确的建议有时很难

//***** 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>';
    }
}
如果将第3个参数设置为true,则可以返回刚刚输入的行的自动id,例如:

//where $sql is a query that will INSERT a row
$query = _db($sql,$params, true);
注意,你正在学习的任何资源都可能非常陈旧。mysql\u查询现在已不推荐使用

这是一个替代品

这是一个使用PDO的启动程序,这比我之前写的安全多了

包含此文件,php脚本需要在其中访问数据库。示例文件名为“database.php”,但这是您的调用。将名称空间从“yourproject”设置为调用项目的名称。更正数据库凭据以适合您的数据库

希望这能帮你省去很多麻烦

我在底部给你举了一些例子。我记得当我刚开始时,得到明确的建议有时很难

//***** 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>';
    }
}
如果将第3个参数设置为true,则可以返回刚刚输入的行的自动id,例如:

//where $sql is a query that will INSERT a row
$query = _db($sql,$params, true);

你已经错过了结束你已经错过了结束感谢所有的帮助家伙-显然,这个例子是为当搜索未设置。当搜索被设置时,我现在需要在搜索之后用相同的结果重新填充同一个表-到目前为止我还没有成功。这是一个简单的做法吗?感谢所有的帮助家伙-显然,这个例子是为当搜索未设置。当搜索被设置时,我现在需要在搜索之后用相同的结果重新填充同一个表-到目前为止我还没有成功。这容易吗?为有用的答案干杯。设置['search']时,如何重新填充表。例如,当用户提交搜索时,我需要在表中填充与其搜索相关的学生…您可以为第一个if和there编写一个else块,并在查询中添加WHERE子句,其余部分与有用答案保持一致。设置['search']时,如何重新填充表。例如,当用户提交搜索时,我需要在表中填充与其搜索相关的学生…您可以为第一个if和there编写一个else块,在查询中添加WHERE子句,其余部分保持不变