【问题标题】:PDO bindValue works for other inputs, why not this one?PDO bindValue 适用于其他输入,为什么不是这个?
【发布时间】:2014-09-24 00:06:56
【问题描述】:

我正在构建一个表单,用户可以在其中更新书籍的属性。有一个动态生成的 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,你们太棒了。

【问题讨论】:

  • 仅供参考,echo- 从 PHP 中提取大量 HTML 只会导致可读性和嵌套引号字符的问题。我建议你退出 PHP 并且只使用它来echo 动态部分
  • 谢谢菲尔。你的意思是这样的: /> ?
  • 是的,但您的 HTML 属性值要加上引号

标签: php mysql sql pdo bindvalue


【解决方案1】:

您的问题是,当 $col 与 Title、Author 或 Description 不同时,换句话说,当 switch 执行默认情况时,您正在执行先前的查询,$nowfield = 184。您不能执行查询。

switch 执行默认情况时,必须“跳转”到下一个值,跳过查询执行。

default:
    echo "Invalid input";
    next($_POST);
    continue 2;
    break;

【讨论】:

    【解决方案2】:

    描述&lt;textarea&gt; 的 HTML 已损坏。你有...

    <textarea ... name=Description id=Description />".$item['Description']."</textarea>
    <!--                                          ^ there's your problem -->
    

    应该在哪里

    <textarea ... name=Description id=Description>".$item['Description']."</textarea>
    

    正如我在评论中提到的,您生成 HTML 的方式必然会导致问题。我建议你采用这种方法...

    // leave the PHP context
    ?>
    <form id="changerform" action="updatesubmit.php" method="post">
        <span id="titlebox">
            Title:
            <input class="attributefield" type="text" id="Title" value="<?= htmlspecialchars($item['Title']) ?>">
            Change This?
        </span>
        <!-- you get the idea -->
    </form>
    

    【讨论】:

    • 谢谢。当我发布问题时,额外的斜线起作用了。它在实际代码中不存在。我认为它可能是在我将
    猜你喜欢
    • 2014-06-12
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 2017-01-18
    • 2016-10-12
    • 2012-07-23
    相关资源
    最近更新 更多