我正在构建一个表单,用户可以在其中更新书籍的属性。有一个动态生成的 HTML 表单,用户可以在其中为“标题”、“作者”和“描述”等内容输入新值,如下所示:
echo "<form id=changerform name=changerform action=updatesubmit.php method=post>";
echo "<span id=titlebox>Title: <input class=attributefield style='border:none' type=text size=100 id=Title value='".$item['Title']."' />Change This?</span> <br>";
echo "<span id=authorbox>Author: <input class=attributefield style='border:none' type=text id=Author value='".$item['Author']."' />Change This?</span><br>";
echo "<span id=description>Description: <textarea class=attributefield style='border:none' rows=9 cols=100 name=Description id=Description >".$item['Description']."</textarea></span>";
echo "<input type='hidden' id='bookidfield' name='bookidfield' value = '".$toChange."' />";
这个表单由一些看起来像这样的 php 处理:
while($nowfield = current($_POST)){
$col = key($_POST);
switch($col){
case 'Title':
$qstring = 'UPDATE Mainbooks SET Title = :slug WHERE ItemID LIKE :bookid;';
break;
case 'Author':
$qstring = 'UPDATE Mainbooks SET Author = :slug WHERE ItemID LIKE :bookid;';
break;
case 'Description':
$qstring = "UPDATE Mainbooks SET Description = :slug WHERE ItemID LIKE :bookid;";
break;
default:
echo "Invalid input";
break;
}//end switch
$upquery = $safelink->prepare($qstring);
$upquery->bindValue(':slug', $nowfield, PDO::PARAM_STR);
$upquery->bindValue(':bookid', $_POST['bookidfield'], PDO::PARAM_INT);
$upquery->execute();
next($_POST);
} //end while
我将其组织为 switch 语句,因为表单中的代码仅传递已在 post 中更改的字段(“bookidfield”输入除外,它具有其中每个项目的唯一键。)开关,只运行必要的查询。
“标题”和“作者”字段工作正常;他们毫无问题地更新到新值。但是,“description”字段总是更新为“bookidfield”的值。如果我进入并手动将 ':bookid' 参数更改为我想要的书的 ID,我可以修复它。
如果我var_dump(_$POST)
似乎通过正确的键值来实现,如下所示:
array(4) { ["Title"]=> string(22) "Gross Boogers and Such" ["Author"]=> string(14) "Franny Panties" ["Description"]=> string(55) "This is the value I want to change the description to!!" ["bookidfield"]=> string(3) "184" }
但是在我的 SQL 表中,它将书名 184 更改为“Gross Boogers and such”,将作者更改为“Franny Panties”,但它会将描述更改为“184”。这与我对 bindValue() 的使用有关,对吧?还是与我的循环有关?表格是如何命名的?我已经看太久了,看不到它是什么。
感谢 Stackoverflow,你们很棒。