【问题标题】:Send individual array results to printer based on id table根据 id 表将单个数组结果发送到打印机
【发布时间】:2013-07-15 18:32:15
【问题描述】:

我正在编写一个简单的 PHP 脚本,它将用户详细信息存储在 MySQL 数据库中。只要查询与姓氏(预定义)匹配,我就可以运行查询并让它返回各个记录。一旦我有了记录,我希望能够在每条记录旁边回显一个打印按钮,以便用户可以单独打印每条记录。

该代码是多个 sn-ps 代码的混搭,除了“打印用户数据”部分外,运行良好。我不是 PHP 的新手,但我也知道足以浏览脚本。这是我到目前为止所得到的。

<?php
//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['keyname']);

//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}

//database connection info
$host = "localhost"; //server
$db = "users"; //database name
$user = "root"; //dabases user name
$pwd = "password1"; //password

//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);

//MYSQL search statement
$query = "SELECT * FROM details WHERE lastname LIKE '%$searchTerm%'";

$results = mysqli_query($link, $query);

/* check whether there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
$output = "";
while($row = mysqli_fetch_array($results))
{

        echo "<tr valign=bottom>";
        echo "<td bgcolor=#ffffff background='img/dots.gif' colspan=6><img     src=img/blank.gif width=1 height=1></td>";
        echo "</tr>";

    echo "<tr valign=center>";
    echo "<td class=tabval><img src=img/blank.gif width=10 height=20></td>";

    echo "<td class=tabval><b>".htmlspecialchars($row['firstname'])."</b> </td>";
    echo "<td class=tabval>".htmlspecialchars($row['lastname'])."&nbsp;</td>";
    echo "<td class=tabval>".htmlspecialchars($row['address'])."</td> ";
    echo "<td class=tabval>".htmlspecialchars($row['phone'])."&nbsp;</td>";
    echo "<button type=\"button\" class=\"formbutton\" onclick=\"printpage('" .     $lastname . "'); \">Print User Data</button>";



    echo "<td class=tabval></td>";
    echo "</tr>";
    $i++;

}
echo "<tr valign=bottom>";
    echo "<td bgcolor=#fb7922 colspan=6><img src=img/blank.gif width=1 height=8></td>";
    echo "</tr>";
echo $output;

}
else
echo "There was no matching record for the name " . $searchTerm;
?>


<br />
<a href="javascript:history.go(-1)">Done</a>

【问题讨论】:

  • 在编写任何更多的 SQL 接口代码之前,请阅读proper SQL escaping,这样您就不会像这里那样创建巨大的SQL injection bugs。您正在使用mysqli,因此您应该使用bind_param 方法添加数据。使用字符串连接没有任何借口。
  • 尽管这个脚本是用于 Intranet 页面的,但我会看到如何加强它的安全性。谢谢
  • 这不仅仅是安全问题。当有人输入Bob's Uncle 作为搜索词时,它不会爆炸。你这里有一个错误,简单明了。

标签: php javascript mysql printing


【解决方案1】:

echo "&lt;button type=\"button\" class=\"formbutton\" onclick=\"printpage('" . $lastname . "'); \"&gt;Print User Data&lt;/button&gt;";

这一行正在调用一个名为“printpage”的 javascript 函数。一旦您在页面上拥有该 javascript 函数,您将更接近您的预期结果。

【讨论】:

  • 不知何故,脚本无法在剪切和粘贴中幸存下来。它在原始代码中,只是由于某种原因没有出现在这里。我按下“打印用户数据”按钮得到的唯一结果是整个页面被发送到打印机,而不是像我需要它们那样的单个记录。
猜你喜欢
  • 2012-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-30
  • 1970-01-01
相关资源
最近更新 更多