繁体   English   中英

php echo'd表中的超链接问题

[英]hyperlink trouble in php echo'd table

我的代码遇到问题,希望有人可以提供帮助。

我正在尝试使用“ 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>

请更换

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

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

尝试打印变量时,主字符串必须用双引号引起来。

如果要在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

<?=$id?><?php echo $id?>简写,PHP版本> = 5.4.0中默认支持 如果启用short_open_tags,也可以在以前的版本中使用它。

如注释中所述,您实际上应该使用mysqli函数,因为不建议使用mysql函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM