在foreach循环中使$ id唯一

时间:2015-09-28 19:28:22

标签: php jquery html mysql foreach

我正在尝试开发一个简单的评论系统。

对于每个新帖子,都会创建一个新的列表项。

listitem的id是AUTO_INCREMENT。弹出窗口应该插入值:post $ id的id,post $ autor的作者和评论用户名的作者

现在的问题是,如果我将元素listone的id设置为行$ row [' id']的id,则id是正确的。但是当我将listitem的id设置为输入元素id的值时,我添加的id或其他内容总是与上次添加的post / list项目相同的id或time。

应该插入帖子的autor的第二个输入元素工作正常。 如何使ID唯一?

    <div data-role="content">                
        <?php
        include("list.php");
        ?>
            <div data-demo-html="true">
                        <ul data-role="listview" data-inset="true" class="ui-listview ui-listview-inset ui-corner-all ui-shadow">

                        <?php foreach ($result as $key => $row): ?>

                            <li id ="listone" name="listone"  data-role="list-divider" role="heading" class="ui-li ui-li-divider ui-bar-b ui-li-has-count ui-first-child"><?php echo $row['date']; ?></li>
                            <li data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="a" >
                            <div class="ui-btn-inner ui-li"><div class="ui-btn-text"><a class="ui-link-inherit"></a><p class="ui-li-aside ui-li-desc"><strong></strong><?php echo $row['time']; ?></p>
                                <p class="ui-li-desc"><strong><?php echo $row['title']; ?></strong></p>
                                <p class="ui-li-desc"><?php echo $row['text']; ?></p>

                                <p class="ui-li-desc"><strong><?php echo $row['town']; ?></strong></p> 
                     <a href="#popupcomment" data-rel="popup" data-position-to="window" data-transition="pop">comment</a>   
                    <button class="commentbtn" data-rel="button" >comment</button>   
            <div id="createcomment" class="comment" data-theme="a" >
    <form data-ajax="false" name="login-form" class="login-form" action="./comments.php" method="post" style="padding:20px 40px;">
        <div class="content">
        <div id="showcomments" data-theme="a" >
    <?php
include("showcomments.php");
?>
    <?php foreach ($comments as $keyComment => $rowComment): ?>
    <div>
    <p class="ui-li-desc"><strong><?php echo $rowComment['username']; ?></strong></p>
                        <p class="ui-li-desc"><?php echo $rowComment['comment']; ?></p>
                        </div>
                <?php endforeach; ?>
</div>
         <input type="hidden" name="id" id="id" value="<?php echo $row['id']; ?>" type="text" />
         <input type="hidden" name="autorpost" id="autorpost" value="<?php echo $row['autor']; ?>" type="text" />
       <textarea rows="1" name="text" id="text" class="foo"></textarea>
        </div>
        <div class="footer">
        <input type="submit" name="submit" value="comment" class="button" data-theme="a"/>          
        </div>  
    </form>
</div>

                            </li>

                <?php endforeach; ?>

        </ul>
                    </div>  
            </div>
        </div>
        </div>

comments.php:

    $hostname='localhost';
        $user='root';
        $password='';

if(isset($_POST["submit"])){                    
                try {
        $dbh = new PDO("mysql:host=localhost;dbname=searchfood", $user, $password);

        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
        // prepare your query
        $query = 'INSERT INTO comments (username, autorpost, comment, id_post, time) VALUES (?, ?, ?, ?, now())';

        $stmt = $dbh->prepare($query);
        // bind variables
        $stmt->execute(array($_COOKIE['username'], $_POST['autorpost'], $_POST['text'], $_POST['id']));
        // pull last insert id
        $new = $dbh->lastInsertId(); 

        // show success message or redirect, whatever you want
        echo "New Record Inserted Successfully";
        $message['success'] = 'Neuer Benutzer (' . htmlspecialchars($_POST['username']) . ') wurde angelegt, <a href="login.php">weiter zur Anmeldung</a>.';
        header("Location: http://".$_SERVER['HTTP_HOST']."/lendsth/main.php", true, 302);

    }
                catch(PDOException $e)
                {
                        echo $e->getMessage();
                }

    }

2 个答案:

答案 0 :(得分:1)

您将每个输入命名为相同。 name="id"因此,无论你提交什么形式,你都会获得最后一个元素。

你应该给name属性一些上下文(因为name attr的值是作为表单输入提交的值)

<?php foreach ($result as $key => $row): ?>
<form action="./comments.php" method="post">
   <input type="hidden" name="authorForm[<?php echo $row['id']; ?>][id]" id="id" value="<?php echo $row['id']; ?>"> </input>  
   <input type="hidden" name="authorForm[<?php echo $row['id']; ?>][autorpost]" id="autorpost" value="<?php echo $row['autor']; ?>"></input>
   <textarea rows="1" name="text" id="text" class="foo"></textarea> 
   <input type="submit" name="submit" value="comment" class="button" data-theme="a"/>          

</form>
<?php endforeach; ?>

编辑跟随第一条评论信息:

以下是一个呈现2种形式序列的scirpt:

<?php
$result = array(
    array(
    'id' => 1,
    'title' => "title_1",
    'text' => "text_1",
    'town' => "town_1",
    'autor' => "autor_1",
    'time' => "time_1"
    ),
    array(
    'id' => 2,
    'title' => "title_2",
    'text' => "text_2",
    'town' => "town_2",
    'autor' => "autor_2",
    'time' => "time_2"
    ),
);
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" ></script>
<ul >
<?php foreach ($result as $key => $row): ?>
    <li id ="listone" name="listone"><?php echo $row['date'];?></li>
    <li>
    <div >
        <?php echo $row['time'];?> <?php echo $row['title']; ?> <?php echo $row['text'];?> <?php echo $row['town'];?>
        <a href="#popupcomment">comment</a>   
    </div>
    <form name="login-form" action="./comments.php" method="post" >
        <div>
            <input type="hidden" name="id" id="id" value="<?php echo $row['id'];?>"/>  
            <input type="hidden" name="autorpost" id="autorpost" value="<?php echo $row['autor'];?>"/> 
            <textarea name="text" id="text" ></textarea>    
        </div>
        <div class="footer">
            <input type="submit" name="submit" value="comment" />          
        </div>  
    </form>
    </li>
<?php endforeach; ?>
</ul>

clicking the 1st clicking the 2nd

因此,您可以在Chrome检查器的屏幕截图中看到。发送正确的值(带有讨厌的表单输入名称,因为它们不提供上下文)

答案 1 :(得分:0)

我可能会考虑给每个帖子一个完全唯一的ID UUID引用它。

你可以在PHP中使用uniqid()(但它并不是完全随机的,但对你的用例没问题)。要获得更安全的随机UUID,您可以使用ramsey/uuid

只需将唯一ID保存为数据库中的字符串即可。