php – PDO bindValue适用于其他输入,为什么不这个?
发布时间:2020-12-13 22:53:21 所属栏目:PHP教程 来源:网络整理
导读:我正在构建一个用户可以更新书籍属性的表单.有一个动态生成的 HTML表单,用户可以在其中为“标题”,“作者”和“描述”之类的内容输入新值,看起来像这样: echo "form id=changerform name=changerform action=updatesubmit.php method=post";echo "span id=t
我正在构建一个用户可以更新书籍属性的表单.有一个动态生成的
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’输入,其中包含每个项目的唯一键.)交换机,只运行必要的查询. “标题”和“作者”字段工作正常;他们更新没有问题的新值.但是,“描述”字段始终更新为“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,你们很棒. 解决方法
问题是,当$col与Title,Author或Description不同时,换句话说,当开关执行默认情况时,您正在使用$nowfield = 184执行上一个查询.您不能执行查询.
当switch执行默认情况时,您必须“跳转”到下一个值,跳过查询执行. default: echo "Invalid input"; next($_POST); continue 2; break; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |