2014-10-26 58 views
-1

我無法找出問題所在。我的McReport.php將顯示我之前從數據庫中輸入的所有數據。我只想顯示使用來自makemc.php的Id的McReport.php中的數據。請幫我找出解決問題的方法。謝謝。PHP從數據庫獲取數據請按照編號

//makemc.php database 
if(isset($_POST['btnSubmit'])){ 
$AddMCQ = "INSERT INTO tblmc(Id,Name,FromDate,ToDate,Reason) VALUES('".$_POST['txtNo']."','".strtoupper($_POST['txtName'])."','".$_POST['txtFrom']."','".$_POST['txtTo']."','".strtoupper($_POST['txtReason'])."')"; 
$AddMCResult = mysql_query($AddMCQ,$link); 

if($AddMCResult) 
{ 
    echo "<script language=\"JavaScript\">\n"; 
    echo "alert('Record Added.');\n"; 
    echo "window.location='McReport.php'"; 
    echo "</script>"; 
} 

//McReport.php 
<?php 
include("database.php"); 
$data = mysql_query("SELECT * FROM tblmc") 
or die(mysql_error()); 

echo "<p align='center'><b>Dr. Ting EE Medical Clinic</b></p>"; 
echo "<p align='center'><font size='2'>MEDICAL CERTIFICATE</font></p>"; 

while($row= mysql_fetch_array($data)) 
{ 
$old_from_date = $row['FromDate']; 
$old_from_date_timestamp = strtotime($old_from_date); 
$new_from_date = date('d/m/Y', $old_from_date_timestamp); 

$old_to_date = $row['ToDate']; 
$old_to_date_timestamp = strtotime($old_to_date); 
$new_to_date = date('d/m/Y', $old_to_date_timestamp); 

echo "<p align='center'>THIS IS TO CERTIFY THAT <u>".$row['Name']."</u> IS RECEIVING MEDICAL</p>"; 
echo "<p align='center'>TREATMENT AND FOR THE PERIOD <u>".$new_from_date."</u> TO <u>".$new_to_date."</u> INCLUSIVE</p>"; 
echo "<p align='center'>HE WILL BE UNFIT TO CONTINUE HIS USUAL OCCUPATION</p>"; 
} 
?> 

回答

0

您必須使用WHERE子句。

SELECT * FROM tblmc WHERE Id='$id'  

您需要的ID $ _ POST [ 'txtNo']makemc.php傳遞給McReport.php(我用的$ id)。

編輯::

makemc.php

if(session_id() == '') {  
    session_start();  
}  
$_SESSION['id'] = $_POST['txtNo'];  

McReport.php

$id = $_SESSION['id']; 
$data = mysql_query("SELECT * FROM tblmc WHERE Id='$id'") or die(mysql_error()); 
+0

因此在makemc.php必須把$ ID = $ _ POST [「txtNo」]; ?? – phpnewbie123 2014-10-26 15:44:13

+0

是的,但是你需要將這個$ id傳遞給McReport.php。 最簡單的方法可能是SESSION。 確保你已經在** makemc.php **中初始化了它,並且使用 'if(session_id()=='){ session_start(); }' 分配$ _SESSION [ '身份證'] '$ _SESSION [ '身份證'] = $ _ POST [ 'txtNo']' 而在** ** McReport.php使用該查詢 '$ id = $ _SESSION ['id']; $ data = mysql_query(「SELECT * FROM tblmc WHERE Id ='$ id'」)或die(mysql_error()); ' – Dangeroustry 2014-10-26 15:53:12

0

定義它/從數據庫retrieveing數據之前,您正在使用$row

... 

include("database.php"); 
$data = mysql_query("SELECT * FROM tblmc") 
or die(mysql_error()); 

$row = mysql_fetch_array($data); // get the first row from the database 

$old_from_date = $row['FromDate']; 
... 

這隻有有效,如果你真的得到1(一)行。該查詢似乎要檢索整個表,讓你無論是由WHERE條款限制或它放入一個循環:

... 

include("database.php"); 
$data = mysql_query("SELECT * FROM tblmc") 
or die(mysql_error()); 

$row = false; 
do { 

    $row = mysql_fetch_array($data); // get the first row from the database 

    $old_from_date = $row['FromDate']; 
    ... // your code here 

} while ($row !== false);