【问题标题】:Problem passing the correct value in ID to another page将 ID 中的正确值传递到另一个页面时出现问题
【发布时间】:2010-09-09 06:54:15
【问题描述】:

我正在使用表单来编辑 cmets 并使用隐藏的输入字段来传递值,

表格是这样的。

<input type="text" name="name" value="<?php echo $name; ?>" class="input" />
<input type="hidden" name="com_name" value="<?php echo $name;?>"/>
<input type="text" name="email" value="<?php echo $email; ?>" class="input" />
<input type="hidden" name="com_email" value="<?php echo $email;?>"/>
<input type="text" name="phone" value="<?php echo $phone; ?>" class="input" />
<input type="hidden" name="com_phone" value="<?php echo $phone;?>"/>
<input type="text" name="location" value="<?php echo $location; ?>" class="input" />
<input type="hidden" name="com_location" value="<?php echo $location;?>"/>
<input type="hidden" name="com_id" value="<?php echo $id; ?>"/>
<textarea name="comment" cols="" rows="" class="comment-msg"><?php echo $comment; ?>
</textarea>
<input type="hidden" name="com_comment" value="<?php echo $comment;?>"/>
<input name="com_update_1" type="submit" class="btn-70" value="" />      

我正在使用 while 循环来迭代表单字段中的值。我的while循环代码是

$query = "SELECT comments.*,
                news.title 
                FROM comments JOIN news ON comments.news_id = news.id
                ORDER BY id DESC LIMIT $from, " . COMM_POST_NUMBER;
      $result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
      $id = $row['id'];
      $date = date("d-F-Y", $row['timestamp']);
      $name = stripslashes($row['name']);
      $email = stripslashes($row['email']);
      $phone = stripslashes($row['phone']);
      $location = stripslashes($row['location']);
      $comment = stripslashes($row['comment']);
      $news_id = $row['news_id'];
      $title = stripslashes($row['title']);
      $approve = $row['approve'];

迭代运行良好,它完美地打印了数据库中的值,并且带有值 ID 的隐藏输入字段在此页面中保存了正确的值,即(cmets.php)。但是当我想将值传递到不同的页面&lt;form action="action.php" method="post"&gt; 时,我的麻烦开始了,当我点击提交时,它只需要第一个 id。即值为 1 的 id,它拒绝从 id 中获取任何其他值。

这里是浏览器查看源代码的代码

<div class="comments-toggle">
 <input type="text" name="name" value="My Name is 16" class="input" />
 <input type="hidden" name="com_name" value="My Name is 16"/>
 <input type="text" name="email" value="check@email.com" class="input" />
 <input type="hidden" name="com_email" value="check@email.com"/>
 <input type="text" name="phone" value="919999999999" class="input" />
 <input type="hidden" name="com_phone" value="919999999999"/>
 <input type="text" name="location" value="Somewhere" class="input" />
 <input type="hidden" name="com_location" value="Somewhere"/>
 <input type="hidden" name="com_id" value="16"/>
 <textarea name="comment" cols="" rows=""class="comment-msg">                              
</textarea>
 <input type="hidden" name="com_comment" value=""/>
  <input name="com_update" type="submit" class="btn-70" value="" />                        
   </div>

这是来自 action.php 的代码

 if( isset($_POST['com_update'])) {
        echo $id = $_POST['com_id'];
    if( isset($_POST['name'])) {
        echo $name = htmlspecialchars(strip_tags(mysql_real_escape_string($_POST['name']))); 
        $query = "UPDATE comments SET name = '$name' WHERE id = '$id'";
        $result = mysql_query($query) or die('Error');
        }

如何让它将正确的 id 值传递给 action.php?

【问题讨论】:

  • 所以您为每条评论创建输入字段?您是否还为每条评论创建一个&lt;form&gt; 元素?如果没有,你必须这样做。如果你把所有东西都放在一个表单中,PHP 总是只会在存在同名字段时取第一个值(除非你将它们指定为数组)。
  • 为什么需要每个隐藏输入来镜像实际输入?
  • 我在表中有大约 20 个具有不同 id 的 cmets,并使用 while 循环填充并将其显示在相应的输入字段中(即,对于每个数据库查询,它将自动输出一个表单)。我的表单结构与 wordpress 相同,我正在使用快速编辑来更改 cmets 值。
  • @Extrakun 隐藏的输入字段是为了检查更改并弹出适当的消息。
  • 但是您已经在数据库中拥有所有这些值。什么样的消息?整个代码对我来说似乎很奇怪

标签: php


【解决方案1】:

实现这一点的方法是将表单字段定义为准备好转换为 PHP 数组:

<input type="hidden" name="com_id[]" value="16"/>

现在,$_POST['com_id'] 应该是一个值数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    相关资源
    最近更新 更多