4

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

4

2 回答 2

1

您的问题是,当 $col 与 Title、Author 或 Description 不同时,换句话说,当开关执行默认情况时,您正在使用 $nowfield = 184 执行上一个查询。您不能执行查询。

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

default:
    echo "Invalid input";
    next($_POST);
    continue 2;
    break;
于 2014-07-31T22:07:06.593 回答
0

您的 HTML 对于Description <textarea>已损坏。你有...

<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-08-01T00:04:42.117 回答