【发布时间】:2010-10-27 00:04:43
【问题描述】:
我有一个表单,我试图用它显示一个带有复选框的表单,然后无论这些复选框是否被选中,都插入到数据库中。然后,每次调用页面时,检查值并将信息附加到 html 表单以适当地将复选框显示为选中或未选中。
我已经修改了下面的代码示例以使其简短,并且我知道我将检查的值分配给“值”,这不会做任何事情。
方法
<input type="checkbox" name="notice" value="checked" checked/>
将复选框显示为已选中,虽然不是有效的 html,但适用于每个浏览器,并且基于示例 here。我宁愿坚持这种方法。
现在,我看不出我的代码有什么问题。
第一步应该是从数据库中检索值,其中只有一行将被检索,因为 article_no 是主键。如果没有检索到任何内容,则只需将变量分配为“”,这会导致未选中复选框。
当该值被选中时,如果它被选中,它应该被插入到数据库中,并在下次查看页面时显示为选中。
现在,这个代码完全失败了。 firebug 没有在控制台中显示任何发送或接收的内容,php 或 mysql 都没有记录错误,尽管字段等正确,但数据库中没有任何内容出现,没有报告 mysql 错误...
有什么问题?
The code:
<?php
error_reporting(E_ALL);
if (isset($_GET["cmd"]))
$cmd = $_GET["cmd"];
else
if (isset($_POST["cmd"]))
$cmd = $_POST["cmd"];
if (isset($_GET["pk"]))
{ $pk = $_GET["pk"];}
if (isset($_POST["deleted"]))
{ $deleted = $_POST["deleted"];}
if (isset($_POST["notice"]))
{ $notice = $_POST["notice"];}
$con = mysqli_connect("localhost","user","pass", "db");
if (!$con) {
echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error();
exit;
}
$con->set_charset("utf8");
$getformdata = $con->query("select * from TEST where ARTICLE_NO = '$pk'");
$checkDeleted = "";
$checkNotice = "";
while ($row = mysqli_fetch_assoc($getformdata))
{
$checkDeleted = $row['deleted'];
$checkNotice = $row['notice'];
}
if($cmd=="submitinfo"){
$statusQuery = "INSERT INTO TEST VALUES (?, ?, ?)";
if ($statusInfo = $con->prepare($statusQuery)) {
$statusInfo->bind_param("sss", $pk, $deleted, $notice);
$statusInfo->execute();
$statusInfo->close();
} else {
print_r($con->error);
}
}
echo "<form name=\"statusForm\" action=\"x.php\" method=\"post\" enctype=\"multipart/form-data\">
<h1>Editing information for auction: ".$pk."</h1>
Löschung Ebay:
<input type=\"checkbox\" name=\"deleted\" value=\"checked\" ".$checkDeleted." align=\"right\"/>
<br />
Abmahnung:
<input type=\"checkbox\" name=\"notice\" value=\"checked\" ".$checkNotice." align=\"left\"/>
<br />
<input type=\"hidden\" name=\"cmd\" value=\"submitinfo\" />
<input name=\"Submit\" type=\"submit\" value=\"submit\" />
</form>";
在实际的表单/页面中,我进行了适当的清理。
一个大问题是没有任何东西被插入到数据库中,也没有返回任何错误!
【问题讨论】:
-
在 PHP 中也添加输入清理 - 客户端清理可以关闭...
-
您的代码容易受到 sql 注入的攻击。在第一个选择查询中对 $pk 使用 bind_param。它也容易受到 xss 注入的影响。将 $pk 嵌入到 html 中时,使用
htmlspecialchars()转义它。