查询仅提交一次


Query only submitting once

出于某种原因,仅当表中还没有具有相同reply_id的行时,此查询才会提交。

编辑:我如何让它继续输入数据?它与重复键有关吗?法典:

if (isset($_POST['submit'])) {
    $blah = $_POST['id'];
    $errors = array();
    if (isset($_POST['comment'])) {
        if (empty($_POST['comment'])) {
            $errors[] = 'Error, try again!';
        }
        if (strlen($_POST['comment']) > 400) {
            $errors[] = 'Comment must be in a 10 to 400 characters range!';
        }
        if (empty($errors)) {
            $q2 = mysqli_query($link, "INSERT INTO reply VALUES($blah, _comment, now(), '$id')");
            header("Location: topic.php?id=$blah");
        } else {
            echo 'You have ' . (count($errors) + 1) . ' errors in your form:<br />';
            foreach ($errors as $error) {
                echo $error . '<br />';
            }
            echo '<a href="new_topic.php">Try again</a>';
        }
    }

形式:

<form action="topic.php" method="POST">
    <textarea name="comment" class="field span6" rows="3"
              placeholder="Content..."></textarea><br/><br/>
    <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
    <div><input type="submit" name="submit" placeholder="Reply"/></div>
</form>

好的,如果你想在你的代码中重复条目。

从代码中删除header("Location: topic.php?id=$blah");。页面再次提交到同一页面,导致每次提交时执行查询。

与未显示多个评论的问题相关

$res2 = mysql_fetch_array($q2) /* will return only one row */

您将必须遍历所有结果,使用

while($res2 = mysql_fetch_array($q2)) {
echo $res2['reply_content'] /*will print each content*/
}

如果 reply_id 是表的主键,则无法插入重复项。

如果确实需要重复的reply_id,请更改(或删除(主键。