我想出了如何解决我的问题,但我真的需要了解为什么我一开始就有这个问题。
发布表单,我想设置变量并根据变量是否设置显示。我做了一个测试代码,因为整页很长。测试代码运行良好,但不在页面上。
第一个在我的页面上不起作用但测试正常的代码是这样的:
<?php
$itype=0;
if (isset($_POST['itype'])) {
$itype=strip_tags($_POST['itype']);
}
?>
<!DOCTYPE html>
<html lang="en">
<form action="test.php?<?php echo time(); ?>" method="post"><input class="invisible" type="radio" name="itype" checked="checked" value="Weapon" /><input class="small" type="submit" value="Weapons" /></form>
<?php
if ($itype==0) {
echo $itype;
}
else {
echo $itype;
}
?>
当我将它添加到我的页面时它会中断。让它在页面上工作的唯一方法是将预设变量 $itype 从 0 更改为“全部”。我需要了解为什么我必须在整页上使用字符串而不是测试脚本?
这是完整的页面,我将其较长但我不知道哪些代码是相关的。贴在这里太长了,所以这里是链接: 库存
我在自学,从来没有任何课堂经验。我真的很想了解这一点,即使这对我的项目来说不是一个紧迫的问题。起初我认为'type'是一个被禁止的变量,所以我把它改成了'itype'。然后我认为它与数字与字符串有关,但这不可能,因为它在简短的测试脚本中工作正常。我已经搜索了手册和这个网站,但找不到答案,或者至少不是我理解的答案。
编辑这是我认为最相关的代码,因为我将其应用于我的页面:
<?php
$itype='All';
if (isset($_POST['itype'])) {
$itype=strip_tags($_POST['itype']);
}
if ($itype=='All')
{
$stmt = $db->prepare("SELECT * FROM inventory WHERE charname=:charname and keep>0 and itemtype != 'Food' ORDER BY equipable desc, itemtype desc, weapontype, itemlevel desc, itemrarity");
$stmt->execute(array(':charname' => $charname));
while($row = $stmt->fetch())
{
include ('../includes/displayinv.php');
}
}
else
{
$stmt = $db->prepare("SELECT * FROM inventory WHERE charname=:charname and keep>0 and itemtype = :type order by weapontype, itemlevel desc, itemrarity");
$stmt->execute(array(':charname' => $charname,
':type' => $itype
));
while($row = $stmt->fetch())
{
include ('../includes/displayinv.php');
}
}
?>
<form action="inventory.php?<?php echo time(); ?>" method="post"><input class="invisible" type="radio" name="itype" checked="checked" value="Weapon" /><input class="small" type="submit" value="Weapons" /></form>
这是一个库存菜单。我想显示所有项目,除非用户选择一个按钮来按项目类型显示库存,比如武器与盔甲。类型是我如何查询 mysql 库存项目表以显示所需项目。当我使用 0s 而不是“全部”作为预设变量实现代码时,表单会分解并继续显示所有项目,按钮不再仅显示所选项目。这是停止工作的 0 代码:
<?php
$itype=0;
if (isset($_POST['itype'])) {
$itype=strip_tags($_POST['itype']);
}
if ($itype==0)
{
$stmt = $db->prepare("SELECT * FROM inventory WHERE charname=:charname and keep>0 and itemtype != 'Food' ORDER BY equipable desc, itemtype desc, weapontype, itemlevel desc, itemrarity");
$stmt->execute(array(':charname' => $charname));
while($row = $stmt->fetch())
{
include ('../includes/displayinv.php');
}
}
else
{
$stmt = $db->prepare("SELECT * FROM inventory WHERE charname=:charname and keep>0 and itemtype = :type order by weapontype, itemlevel desc, itemrarity");
$stmt->execute(array(':charname' => $charname,
':type' => $itype
));
while($row = $stmt->fetch())
{
include ('../includes/displayinv.php');
}
}
?>
<form action="inventory.php?<?php echo time(); ?>" method="post"><input class="invisible" type="radio" name="itype" checked="checked" value="Weapon" /><input class="small" type="submit" value="Weapons" /></form>