2017-02-10 52 views
1

我有一個摘要頁面,它將顯示所有名爲readmsg.php的客戶名稱,每個客戶在MySQL數據庫中都有一個唯一的msgid。其他頁面可以查看它們的詳細信息,稱爲readmsgdetail.php。在PHP中使用id顯示URL herf鏈接

但是,我在使用msgid顯示一個客戶的詳細信息時遇到問題。現在,我的readmsgdetail.php可以顯示所有客戶的所有細節。

我readmsg.php代碼:

 <div class="row"> 
     <?php 
     $stmt = $DB_con->prepare('SELECT firstname,lastname,phone,enquiry FROM user_message ORDER BY msgid DESC'); 
     $stmt->execute(); 

     if ($stmt->rowCount() > 0) { 
      while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
       extract($row); 
       ?> 
       <div class="col-xs-12"> 
        <p><a class="page-header" href="readmsgdetail.php?msgdetail_id=<?php echo $row['msgid']; ?>"><?php echo $lastname . $firstname; ?></a></p> 
       </div>  
       <?php 
      } 
     } else { 
      ?> 
      <div class="col-xs-12"> 
       <div class="alert alert-warning"> 
        <span class="glyphicon glyphicon-info-sign"></span> &nbsp; No Data Found ... 
       </div> 
      </div> 
      <?php 
     } 
     ?> 
    </div> 

我readmsgdetail.php代碼:

<?php 
require_once 'dbconfig.php'; 
//////------------------------------------------------ 
session_start(); 
if (empty($_SESSION) || empty($_SESSION['login_id'])) { 
    header('location:login.php'); 
}; 
echo"Welcome!! " . $_SESSION['login_id'] . " "; 
echo '<a href="logout.php">log out</a>'; 
//----------------------------------------------------- 

if (isset($_REQUEST['msgdetail_id']) && !empty($_REQUEST['msgdetail_id'])) { 
    $id = $_REQUEST['msgdetail_id']; 
} else { 

    header("Location: readmsg.php"); 
} 
?> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" /> 
     <title></title> 
     <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/> 


     <link href="../bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css"/> 
    </head> 
    <body> 




     <div class="container"> 


      <div class="page-header"> 
       <h1 class="h2">User review <a class="btn btn-default" href="readmsg.php"> all reviews </a></h1> 
      </div> 



      <?php 
      $stmt = $DB_con->prepare('SELECT firstname,lastname,phone,enquiry FROM user_message ORDER BY msgid DESC'); 
      $stmt->execute(); 

      if ($stmt->rowCount() > 0) { 
       while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
        extract($row); 
        ?> 
        <form method="post" enctype="multipart/form-data" class="form-horizontal"> 




         <table class="table table-bordered table-responsive"> 

          <tr> 
           <td><label class="control-label">Name.</label></td> 
           <td><input class="form-control" type="text" name="name" value="<?php echo $lastname . $firstname; ?>" required /></td> 
          </tr> 



          <tr> 
           <td><label class="control-label">Phone </label></td> 
           <td><input class="form-control" type="text" name="phone" value="<?php echo $phone; ?>" required /></td> 
          </tr> 

          <tr> 
           <td><label class="control-label">Feedback/Enquiry.</label></td> 

           <td><input class="form-control" type="text" name="comment" value="<?php echo $enquiry; ?>" required /></td> 

          </tr> 

          <tr> 
           <td colspan="2"> 
            <a class="btn btn-default" href="readmsg.php"> <span class="glyphicon glyphicon-backward"></span> back </a> 

           </td> 
          </tr> 
         </table> 
        </form>  
        <?php 
       } 
      } else { 
       ?> 
       <div class="col-xs-12"> 
        <div class="alert alert-warning"> 
         <span class="glyphicon glyphicon-info-sign"></span> &nbsp; No Data Found ... 
        </div> 
       </div> 
       <?php 
      } 
      ?> 

     </div> 
    </body> 
</html> 

的MySQL stucture: enter image description here

目前的結果爲網頁:

enter image description here

後,我改變我的select語句

$stmt = $DB_con->prepare("SELECT firstname,lastname,phone,enquiry FROM user_message where msgid = 'msgdetail_id'"); 

它顯示 '未發現數據'。

+0

將'WHERE'子句添加到'SELECT'。 –

+0

我更改爲「$ stmt = $ DB_con-> prepare(」SELECT firstname,lastname,phone,inquiry FROM user_message where msgid ='msgdetail_id'「);」它沒有顯示數據。 – xhinvis

+0

'這裏的msgid ='msgdetail_id''是因爲msgid是int而你試圖傳遞一個字符串。 –

回答

0

在readmsg.php的代碼應該改爲:

$stmt = $DB_con->prepare('SELECT msgid, firstname,lastname,phone,enquiry FROM user_message ORDER BY msgid DESC'); 

在readmsgdetail.php的代碼應該改爲:

$stmt = $DB_con->prepare("SELECT firstname,lastname,phone,enquiry FROM user_message where msgid=$id"); 

現在這是沃金! Thx從評論的幫助!

相關問題