PHP帮助获取数据

时间:2013-03-24 23:40:25

标签: php

我已经制作了这个Printers.php页面,其中列出了数据库中的所有信息。 我有一个模板文件printerbymodel.php,它列出了单个打印机的信息。 基本上我遇到的问题是如何点击其中一个列出的打印机并将其带到printerbymodel.php页面。

以下是我的网页

PrintFunctions.php

  function getPrinters2() {
$query = mysql_query("SELECT * FROM printers") or die (mysql_error());
if (mysql_num_rows($query) == 0) {
echo "<tr><td colspan=\"3\">No Printers Were Found</td></tr>";
} else {
while($post = mysql_fetch_assoc($query)) {
$bran=$post['Brand'];
$mod=$post['Model'];
$off=$post['Office'];
$loc=$post['Location'];
$ip=$post['IPAddress'];
$mac=$post['MACAddress'];
$Ink=$post['Ink'];
$Stock=$post['Stock'];
$id=$post['id'];
echo "<tr><td>" . $bran . "</td><td>" .  '<a href="/web/printer%202/printer/admin/printerbymodel.php?id='.$post['Model'].'">'.$post['Model'].'</a>' . "</td><td>" . $off . "</td><td>" . $loc . "</td><td>" . $ip . "</td><td>" . $mac. "</td><td>" .  $Ink . "</td><td>" . $Stock . "</td><td><a href=\"delete.php?id=" . $id . "\">Delete</a><br /><a href=\"edit.php?id=" . $id . "\">Edit</a></td></tr>";
}
}
}

Printerbymodel.php

    <?php
mysql_connect("localhost", "root", "") or
    die("Could not connect: " . mysql_error());
mysql_select_db("whc_tech");

$result = mysql_query("SELECT id, Brand, Model, Office, Location, IPAddress, MACAddress, Ink  FROM printers");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {


    $bran = $row['Brand']; 
$mod = $row['Model']; 
    $off = $row['Office']; 
$loc = $row['Location']; 
$ip = $row['IPAddress']; 
$mac = $row['MACAddress']; 
$Ink = $row['Ink']; 
}

mysql_free_result($result);
?>
<html>
    <head>
    <title><?php echo $mod ?></title>
    </head>
    <body>
      <table>
<tr>
<td>Brand</td><td><?php echo $bran; ?></td>
</tr>
<tr>
<td>Model:</td><td><?php echo $mod; ?></td>
</tr>
<tr>
<td>Office:</td><td><?php echo $off; ?></td>
</tr>
<tr>
<td>Location:</td><td><?php echo $loc; ?></td>
</tr>
<tr>
<td>IP Address:</td><td><?php echo $ip; ?></td>
</tr>
<tr>
<td>MAC Address:</td><td><?php echo $mac; ?></td>
</tr>
<tr>
<td>Ink Type:</td><td><?php echo $Ink; ?></td>
</tr>

</table>
    </body>
</html>

Printers.php

 <?php include('includes/PrintFunctions.php');?>

<html>


    <head>

        <title>Printer List</title>
    </head>

    <body bgcolor="f8e2e2">

<style type="text/css">
table.sample {
    border-width: 1px;
    border-spacing: 5px;
    border-style: outset;
    border-color: black;
    border-collapse: separate;
    background-color: rgb(250, 240, 230);

}
table.sample th {
    border-width: 1px;
    padding: 1px;
    border-style: dotted;
    border-color: gray;
    background-color: rgb(255, 250, 250);
    -moz-border-radius: ;
}
table.sample td {
    border-width: 1px;
    padding: 1px;
    border-style: dotted;
    border-color: gray;
    background-color: rgb(255, 250, 250);
    -moz-border-radius: ;
}
</style>

<table class="sample" align="center">
    <thead>
<tr>

            <td><b>Brand</b></td>
            <td><b>Model</b></td>
            <td><b>Office</b></td>
            <td><b>Location</b></td>
            <td><b>IP Address</b></td>
            <td><b>MAC Address</b></td>
            <td><b>Ink Type</b></td>
            <td><b>Ink Stock</b></td>



            <td><b>Action</b></td>
        </tr>

    </thead>
        <tbody>
<?php 
getPrinters2(); 
?>

    </tbody>

</table>





?>


    </body>
</html>

现在,当打印机信息被添加到数据库中时,它们会被列在printers.php中,而Model是一个超链接,这是我被卡住的地方。完全失去了当我点击它时,我将被带到该特定打印机的printerbymodel.php页面,该页面将显示该信息。提前致谢。

2 个答案:

答案 0 :(得分:0)

我不是100%肯定你要求的,但我的猜测是你要求如何编码printerbymodel.php来输出特定的模型?

在printers2()中,您创建了超链接:

echo "<tr><td>" . $bran . "</td><td>" .  '<a href="/web/printer%202/printer/admin/printerbymodel.php?id='.$post['Model'].'">'.$post['Model'].'</a>' . "</td><td>" . $off . "</td><td>" . $loc . "</td><td>" . $ip . "</td><td>" . $mac. "</td><td>" .  $Ink . "</td><td>" . $Stock . "</td><td><a href=\"delete.php?id=" . $id . "\">Delete</a><br /><a href=\"edit.php?id=" . $id . "\">Edit</a></td></tr>";

您应该将id更改为指向当前记录(而不是模型)的实际Id,如下所示:

echo "<tr><td>" . $bran . "</td><td>" .  '<a href="/web/printer%202/printer/admin/printerbymodel.php?id='.$post['id'].'">'.$post['Model'].'</a>' . "</td><td>" . $off . "</td><td>" . $loc . "</td><td>" . $ip . "</td><td>" . $mac. "</td><td>" .  $Ink . "</td><td>" . $Stock . "</td><td><a href=\"delete.php?id=" . $id . "\">Delete</a><br /><a href=\"edit.php?id=" . $id . "\">Edit</a></td></tr>";

我认为您应该将variablename $ post更改为其他内容,因为它很容易与$ _POST混淆...

你必须做这样的事情(修改现有的一些getPrinter2()。(我没有做过任何验证或者无论如何......)

printerbymodel.php

<?php
$id=$_GET['id']; //Got from hyperlink

getPrintersInfo($id);
function getPrintersInfo($id) {
    $query = mysql_query("SELECT * FROM printers WHERE id=$id") or die (mysql_error());

    if (mysql_num_rows($query) == 0) {
    echo "<tr><td colspan=\"3\">No Printers Were Found</td></tr>";
    } else {


    while($post = mysql_fetch_assoc($query)) {
    $bran=$post['Brand'];
    $mod=$post['Model'];
    $off=$post['Office'];
    $loc=$post['Location'];
    $ip=$post['IPAddress'];
    $mac=$post['MACAddress'];
    $Ink=$post['Ink'];
    $Stock=$post['Stock'];
    $id=$post['id'];

    //Output information about this specific product...
    echo 'id=' . $id . '<br />';
    echo 'Model=' . $mod; 
    //etc...
}
}
}

答案 1 :(得分:0)

请参阅已发布的print functions.php的第16行,它回显了一个包含1台打印机信息的表格行,重点关注此部分:

'<a href="/web/printer%202/printer/admin/printerbymodel.php?id='. $post['Model'].'">'.$post['Model'].'</a>'

(1)我怀疑这是错误的,因为它设置了id = model,所以更改此行,请参阅***在哪里进行更改!

'<a href="/web/printer%202/printer/admin/printerbymodel.php?id='. $post[***'id'].'">'.$post['Model'].'</a>'

(2)现在,这是创建一个类似的链接:printerbymodel.php?id=1234。在printerbymodel.php中,您现在需要从网址

获取该ID
if (isset($_GET['id']) $id = $_GET['id'];

(3)然后,从db:

中提取记录
$sql = "SELECT * FROM printers WHERE id=$id";

// get the record from your db by committing $sql
// read the result and echo it.