mysql数据的每一行都有超链接

时间:2012-03-22 12:32:29

标签: php mysql

<?php

require 'database.php'; 
$query = "SELECT id, date, ponumber FROM so";

$result = $mysqli->query($query) or die(mysqli_error($mysqli));

if ($result) {
  echo "<form method='post' action='delete.php'>"; 

  echo "<table cellspacing='0' cellpadding='15' border='1'>
  <th >DELETE</th>
  <th >VIEW</th>
  <th >ID</th>
  <th >DATE</th>
  <th >PO NUMBER</th>";

  while ($row = $result->fetch_object()) {

$date      = $row->date ;
$ponumber  = $row->ponumber;
$id        = $row->id;
//put each record into a new table row with a checkbox
echo "<tr>
    <td>
        <input type='checkbox' name='checkbox[]' id='checkbox[]'  value=$id />
    </td>
     <td>
        $id
     </td>
     <td>
        <a href="view.php">view</a>
     </td>
    <td>
         $date
    </td>
<td>
       $ponumber
     </td>
  </tr>"; 
    }

   echo "</table><p><input id='delete' type='submit' class='button' name='delete' 

       value='Delete Selected Items'/></p></form>";}
?>

我有一种在线订单,可以让销售代表输入销售订单, 我已经使用上面的代码完成了插入和删除现在我希望每一行都是一个超链接,这样当他们点击查看时,它将只显示已被点击的行,如果你点击上面的代码:查看“所有细节将会显示,我如何只显示我将点击的行将显示记录的详细信息!

1 个答案:

答案 0 :(得分:1)

您需要传递网址中的ID,如果存在,则需要读取

e.g。

<?php
require 'database.php'; 
$query = "SELECT id, date, ponumber FROM so";
/* Edit 1 */
if (!empty($_GET['id'])) { 
    $query .= " WHERE id = " . mysql_real_escape_string($_GET['id']);
}
/* Edit 1 end */
$result = $mysqli->query($query) or die(mysqli_error($mysqli));
if($result) {
    echo "<form method='post' action='delete.php'>"; 
    echo "<table cellspacing='0' cellpadding='15' border='1'>
            <th >DELETE</th><th >VIEW</th><th >ID</th><th >DATE</th><th >PO NUMBER</th>";
    while ($row = $result->fetch_object()) {
        $date = $row->date ;
        $ponumber = $row->ponumber;
        $id = $row->id;
        //put each record into a new table row with a checkbox
        echo "<tr>
                  <td><input type='checkbox' name='checkbox[]' id='checkbox[]'  value=$id /></td>
                  <td>$id</td>
                  <td>";
            /* Edit 2 */
            echo "<a href='view.php?id=$id'>view</a>";
            /* Edit 2 End */
            echo "</td>
                  <td>$date</td>
                  <td>$ponumber</td></tr>"; 
    }
    echo "</table><p><input id='delete' type='submit' class='button' name='delete' value='Delete Selected Items'/></p></form>";}
?>

风格建议:

不要/停止这样做:

echo "<form method='post' action='delete.php'>"; 
echo ...
while

您回忆的是静态字符串。相反,做:

?>
<form method='post' action='delete.php'>
...
<?php
while

它更容易阅读和维护。