Php 是否可以在isset$\u post['submit']中添加变量

Php 是否可以在isset$\u post['submit']中添加变量,php,if-statement,isset,Php,If Statement,Isset,有可能这样做吗 if (isset($_POST['Submit_'.$_POST['ID']])) { } 或 我想这样做,这样我可以得到一个不同的提交按钮,为所有的帖子,因为我有一个评论系统,在一个帖子系统。。所以在所有的帖子上都有一个新的提交按钮 submitbutton的制作方式如下: echo "<button type='submit' class='commentbtn' name='commentSubmit_".$row['ID']."'></button

有可能这样做吗

 if (isset($_POST['Submit_'.$_POST['ID']])) {
}

我想这样做,这样我可以得到一个不同的提交按钮,为所有的帖子,因为我有一个评论系统,在一个帖子系统。。所以在所有的帖子上都有一个新的提交按钮

submitbutton的制作方式如下:

echo "<button type='submit' class='commentbtn' name='commentSubmit_".$row['ID']."'></button>
完成setStatus函数,内置注释表单:


是的,这是可能的,而且您的第一个变体是正确的

我认为更好的办法是为每个注释字段创建一个单独的表单,并添加ID作为隐藏输入

大概是这样的:

<form method="post" action="/something">
<textarea name="comment">
<input type="hidden" name="id" value="$row['ID']">
<button type="submit" name="submit"></button>
</form>

我无法真正理解代码中的逻辑,但似乎您的过程过于复杂了。。。我假设您的操作并没有像您所展示的那样实际指向该函数,而是指向一个具有该函数的页面。。。无论如何如果你有这张表格

 echo "
<div class='commentform'>
    <form id='comment_form_".$row1['sid']."' action='setcomment.php' method='POST'>
        <textarea name='commentText' class='commenttext' placeholder='Comment this..'></textarea>
        <input type='hidden' name='uname' value='".$_SESSION['name']."'>
        <input type='hidden' name='uid' value='".$_SESSION['id']."'>
        <input type='hidden' name='uimg' value='".$_SESSION['profile_img']."'>
        <input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
        <input type='hidden' name='sid' value='".$row1['sid']."'>
        <button class='commentbtn' name='commSubmit_".$row1['sid']."' type='submit' style='border: 0; background: transparent'>
            <img src='images/comment-icon.png'  height='24'' alt='comment' title='Comment''  />
        </button>
    </form>
</div>";

是的,你的第一个表达是正确的。查看更多信息。虽然我会使用检查表单提交

// check if request method is post
if ($_SERVER['REQUEST_METHOD'] === 'POST') { 
    // .... your code
}
此外,我还查看了您的代码。我认为错误是HTML格式的

echo "<div class='commentform'><form id='comment_form_".$row1['sid']."' action='".setComment($conn)."' method='POST'>
它使函数运行4次,并记录4次。请替换action=或action='processComent.php'//进程代码的位置

同样地,你也可以这样做

<form class='delete-form' method='POST' action='".deleteStatus($conn)."'>

同样,删除时也会发生同样的事情。

但是注释字段在生成所有帖子的while循环中生成:我就是这么想的。你只需要为每个评论创建一个不同的表单并添加隐藏的输入。那么我应该如何设置if-isset-then呢?如果设置了$\u POST['submit'],那么你就可以从隐藏的输入中找出评论是为哪个帖子写的:$\u POST['id']我的问题是,例如,如果我在页面上有4篇文章,当我一次按一个提交按钮时,它会将数据存储在我的数据库中4次是的,但是当然,在这个测试中使用它之前,你应该检查$_POST['ID']是否确实存在,我将它放在表单中,就像这个echo一样@Riggsfolli尝试过,现在我没有收到错误消息,但现在数据库中没有存储任何内容..好的,我发现了一个错误,并且已修复,但仍在数据库中保存了4次我不明白..我想我没有足够的信息知道你在做什么。。。如果我能看到您的表单和插入查询,我可能会指出问题所在。唯一的建议是4x insert表示它处于循环中,可能foreachi现在已经添加了表单和插入查询:我不能使用setComments$conn函数?是的,您可以使用setComments$conn函数。但是,不是作为action=setComments$conn。HTML标记上的action属性意味着它是提交表单后处理的脚本的位置。不是函数名。例如:如果将函数setComments$conn放入processComment.php中,则将其放入标记中。希望这能说明问题。如果没有,请检查表单中的“是”中的“操作”属性!对不起,我忘了按有用的回答按钮^^,再次感谢!
 echo "
<div class='commentform'>
    <form id='comment_form_".$row1['sid']."' action='setcomment.php' method='POST'>
        <textarea name='commentText' class='commenttext' placeholder='Comment this..'></textarea>
        <input type='hidden' name='uname' value='".$_SESSION['name']."'>
        <input type='hidden' name='uid' value='".$_SESSION['id']."'>
        <input type='hidden' name='uimg' value='".$_SESSION['profile_img']."'>
        <input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
        <input type='hidden' name='sid' value='".$row1['sid']."'>
        <button class='commentbtn' name='commSubmit_".$row1['sid']."' type='submit' style='border: 0; background: transparent'>
            <img src='images/comment-icon.png'  height='24'' alt='comment' title='Comment''  />
        </button>
    </form>
</div>";
if(isset($_POST['sid']) && isset($_POST['commSubmit_'.$_POST['sid']])){
     setComment($conn);  
}
 function setComment($conn) {

        $uname = $_POST['uname'];
        $uid = $_POST['uid'];
        $date = $_POST['date'];
        $comment = $_POST['commentText'];
        $uimg = $_POST['uimg'];
        $sid = $_POST['sid'];

        $sql = "
            INSERT INTO 
                status_comments 
            (
                uid, sid, uname, comment, uimg, date
            ) 
            VALUES 
            (
                '".mysqli_real_escape_string($conn,$uid)."', 
                '".mysqli_real_escape_string($conn,$sid)."', 
                '".mysqli_real_escape_string($conn,$uname)."', 
                '".mysqli_real_escape_string($conn,$comment)."', 
                '".mysqli_real_escape_string($conn,$uimg)."', 
                '".mysqli_real_escape_string($conn,$date)."'
            )";
        $result = mysqli_query($conn, $sql);

  }
// check if request method is post
if ($_SERVER['REQUEST_METHOD'] === 'POST') { 
    // .... your code
}
echo "<div class='commentform'><form id='comment_form_".$row1['sid']."' action='".setComment($conn)."' method='POST'>
<form class='delete-form' method='POST' action='".deleteStatus($conn)."'>