查询仅提交一次

时间:2013-10-12 10:25:09

标签: php

由于某种原因,如果在表格内部还没有一行具有相同的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>

2 个答案:

答案 0 :(得分:1)

好的,如果您希望代码中有重复的条目。

从代码中删除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*/

}

答案 1 :(得分:0)

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

如果您 想要重复reply_id,请更改(或删除)主键。