【问题标题】:hyperlink trouble in php echo'd tablephp echo'd表中的超链接问题
【发布时间】:2014-06-06 02:54:55
【问题描述】:

我的代码有问题,希望有人能提供帮助。

我正在尝试使用“php echo”调用信息以以表格形式显示信息,除了无法识别 $id 的链接外,它可以正常工作。如果我不把它放在表格中,它可以正常工作,但它不美观。

任何建议将不胜感激!

<?php
session_start();
if(!isset($_SESSION['name'])){
    header("location: ../index.php");
exit();
}
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
include_once("../scripts/connect.php");
// Delete Item Question to Admin, and Delete Product if they choose
if (isset($_GET['deleteid'])) {
echo 'Do you really want to delete messages with ID of ' . $_GET['deleteid'] .'? <a     href="admin_messages.php?yesdelete=' . $_GET['deleteid'] . '">Yes</a> | <a href="admin_messages.php">No</a>';
exit();
}
if (isset($_GET['yesdelete'])) {
    // delete from database
$id_to_delete = $_GET['yesdelete'];
$sql = mysql_query("DELETE FROM `mystore`.`messages` WHERE `messages`.`id` =     '$id_to_delete' LIMIT 1") or die (mysql_error());
}
$messages = "";
$sql = mysql_query("SELECT * FROM messages ORDER BY msg_date DESC LIMIT 20");
$count = mysql_num_rows($sql);
if($count > 0){
while($row = mysql_fetch_array($sql)){
    echo '<tr>';
    echo '<td>'.$row['msg_name'].'</td>';
    echo '<td>'.$row['msg_email'].'</td>';
    echo '<td>'.$row['msg_subject'].'</td>';
    echo '<td>'.$row['msg_date'].'</td>';
    echo '<td><a href="mailto: '.$row['msg_email'].'">Reply</a></td>';
    echo '<td><a href="admin_messages.php?deleteid=$id">Delete</a></td>';
    echo '</tr>';
}
}else{
$messages = "<b>There are no messages in the database at this moment</b>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Admin Messages</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="style/forms.css" media="screen">
<link rel="stylesheet" href="style/main.css" media="screen">
</head>
 <body>
    <div id="main_wrapper">
        <?php include_once("templates/tmp_header.php"); ?>
        <?php include_once("templates/tmp_nav.php"); ?>   
        <section id="main_content">
        <h2 class="page_title">Messages</h2>
        <br/>
        <table width="730" cellspacing="0" cellpadding="3" border="1">
            <tr>
                <td align="center" width="100">From</td>
                <td align="center" width="300">Email</td>
                <td align="center" width="300">Subject</td>
                <td align="center" width="100">Date</td>
                <td align="center" width="100">Actions</td
            ></tr>
            <?php echo $messages; ?>
        </table>
    </section>
    <?php include_once("templates/tmp_aside.php"); ?>
    <?php include_once("templates/tmp_footer.php"); ?>
</div>

【问题讨论】:

标签: php hyperlink echo


【解决方案1】:

请更改

echo '<td><a href="admin_messages.php?deleteid=$id">Delete</a></td>';

echo "<td><a href='admin_messages.php?deleteid=$id'>Delete</a></td>";

当试图打印出一个变量时,主字符串必须用双引号括起来。

【讨论】:

    【解决方案2】:

    如果你想在 PHP 中插入变量,你需要使用双引号。 echo '$id' 将直接打印$id,而echo "$id" 将打印变量的值。但是,我会推荐一种替代方法。不要在不需要的地方使用 PHP。没必要这么用echo

    我会将循环的内容更改为:

    ?>
    <tr>
      <td><?=$row['msg_name']?></td>
      <td><?=$row['msg_email']?></td>
      <td><?=$row['msg_subject']?></td>
      <td><?=$row['msg_date']?></td>
      <td><a href="mailto:<?=$row['msg_email']?>">Reply</a></td>
      <td><a href="admin_messages.php?deleteid=<?=$id?>">Delete</a></td>
    </tr>
    <?php
    

    &lt;?=$id?&gt;&lt;?php echo $id?&gt; 的简写,在 PHP 版本 >=5.4.0 中为 supported by default。如果您启用了 short_open_tags,您也可以在以前的版本中使用它。

    如 cmets 中所述,您确实应该使用 mysqli 函数,因为 mysql 函数已被弃用。

    【讨论】:

      猜你喜欢
      • 2020-04-24
      • 2010-09-23
      • 1970-01-01
      • 1970-01-01
      • 2010-11-25
      • 2013-07-29
      • 2017-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多