PHP从数据库中获取数据遵循ID

时间:2014-10-26 15:22:04

标签: php database

我无法找出问题所在。我的McReport.php将显示我之前从数据库键入的所有数据。我只想在McReport.php中显示使用makemc.php中Id的数据。请帮我找出解决问题的方法。谢谢。

//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>"; 
} 
?>

2 个答案:

答案 0 :(得分:0)

您必须使用WHERE子句。

SELECT * FROM tblmc WHERE Id='$id'    

您需要将 makemc.php 中的ID $ _ POST ['txtNo'] 传递给 McReport.php (我用过<强> $ ID )。

EDIT ::

<强> 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());

答案 1 :(得分:0)

在定义数据/从数据库中检索数据之前,您正在使用$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 );