2014-04-21 29 views
0

我“有我的代码的麻烦米希望有人可以提供帮助。超级链接麻烦

我试图使用呼叫信息‘PHP回声’以表格形式显示信息和它的作品,除了对于不认识的$ 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> 

+0

**注 - MYSQL功能已被取消。请使用MYSQLI或PDO使用准备好的语句。 http://www.php.net/manual/en/book.pdo.php** – user1475632

回答

0

试图打印出一个变量的主串必须被包裹在双引号时,请更改

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

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

0

如果要在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 supported by default。如果启用了short_open_tags,您也可以在以前的版本中使用它。

正如评论中所述,您应该真的使用mysqli函数,因为mysql函数已被弃用。