在while循环之外使用$ _GET,其中定义了['']

时间:2016-04-16 12:52:50

标签: php get

例如,我有以下代码:

<?php
$query = mysqli_query($connect, "SELECT * FROM user_thoughts WHERE added_by='$user' ORDER BY id DESC");

while ($row = mysqli_fetch_array($query)) {
    $thought_id      = $row['id'];
    $message_content = $row['message'];
    $date_of_msg     = $row['post_details'];
    $thoughts_by     = $row['added_by'];
    $attachent       = $row['attachment'];
    $shared          = $row['shared'];

    // for each post a user has made, a new div will be echo's
    echo    "
            <div class='message_wrapper'>
                // all content here which displays the message and author.
                // consider this anchor link, and see $_GET approach below.
                <a href='/inc/del_post.php?id=$thought_id>'>Delete </a> 
                <div id='toggleComment$thought_id' class='new_comment'>
                    <form action='' method='post' enctype='multipart/form-data'>
                        <table>
                            <tr>
                                <td>
                                    <textarea id='txtarea' name='comment_msg' cols='80' maxlength='180' placeholder=' add your comment...'></textarea> 
                                </td>
                                <td>
                                    <input id='send' type='submit' name='send_comm' value='Share'/> 
                                </td>
                            </tr>
                         </table> 
                    </form>
                </div>
            </div>";
} // while loop closed
// sending comments to database 
$comment = htmlentities(trim(strip_tags(@$_POST['comment_msg'])));
$comment = mysqli_real_escape_string($connect, $comment);

// if button is pressed, do this...
if(isset($_POST['send_comm'])){
    if (!empty ($comment)){
        $insert_comment = mysqli_query ($connect, "INSERT INTO user_comments VALUES ('','$comment','$username','$user','0','$thought_id')");
        header ("Location: /profile_page/$user");
    } 
}
?>

之前,我在while循环中进行了'send_comm'处理,当我用来提交表单时,comment将被添加到所有用户帖子中。例如,Alice发了两个帖子,我给一个帖子添加了评论,两个帖子都会显示该消息(以及db中的两个新行)。

现在,为了解决上述问题,我在while循环之外放了'send_comm'处理,当然,这个$thought_id(在我的INSERT中)将是未定义的。此外,将它放在while循环之外,无法知道哪个thought_id被赋值。所以要解决这个问题,我尝试使用$_GET

$thought_id_from_anchor = $_GET ['id'];

// if button is pressed, do this...
if(isset($_POST['send_comm'])){
    if (!empty ($comment)){
        $insert_comment = mysqli_query ($connect, "INSERT INTO user_comments VALUES ('','$comment','$username','$user','0','$thought_id_from_anchor')");
        header ("Location: /profile_page/$user");
    } 
} 

但是,当然,因为它在while循环之外,我在id上得到一个未定义的错误。

我只需将评论添加到正在添加的$thought_id

3 个答案:

答案 0 :(得分:1)

您只需在表单中添加hidden输入,其中包含$thought_id的值:

<form action='' method='post' enctype='multipart/form-data'>

    <input type='hidden' name='thought_id' value='$thought_id'>

    <table>
        <tr>
            <td>
                <textarea id='txtarea' name='comment_msg' cols='80' maxlength='180' placeholder=' add your comment...'></textarea> 
            </td>
            <td>
                <input id='send' type='submit' name='send_comm' value='Share'/> 
            </td>
        </tr>
     </table> 
</form>

然后,当提交表单时,您可以使用thought_id为您的查询访问$_POST的值(也清理了一下):

// if button is pressed, do this...
if (isset($_POST['send_comm'])) {

    $_POST = array_map('trim', $_POST);

    if (!empty($_POST['thought_id']) &&
        !empty($_POST['comment_msg'])) {

        $comment = htmlentities(strip_tags($_POST['comment_msg']));
        $comment = mysqli_real_escape_string($connect, $comment);
        $thought_id = mysqli_real_escape_string($connect, $_POST['thought_id']);

        $insert_comment = mysqli_query ($connect, "INSERT INTO user_comments VALUES ('','$comment','$username','$user','0','$thought_id')");
        header ("Location: /profile_page/$user");
    }
    else {
        // empty fields; handle this accordingly
    }
}

答案 1 :(得分:0)

INSERT INTO user_comments VALUES ('','$comment'

那个空字符串是什么?

我认为这是ID,因此,ID只接受整数值,您可以替换为null,或者将其删除。

INSERT INTO user_comments VALUES (null,'$comment'

INSERT INTO user_comments VALUES ('$comment'

答案 2 :(得分:0)

如果你想使用循环的id out,你必须将它&gt;分配给一个全局变量。你需要在全局范围中声明一个变量&gt;然后在中使用global关键字while循环。一旦你这样做,你可以使用think_id变量任何你选择。   

  

<?php $query = mysqli_query($connect, "SELECT * FROM user_thoughts WHERE added_by='$user' ORDER BY id DESC"); $thought_id; // Declare the variable outside of the while loop in the global scope while ($row = mysqli_fetch_array($query)) { global $thought_id = $row['id']; /*use the global keyword to assign the the value of this variable to the global variable and you will be able to use it out side of the while loop */ $message_content = $row['message']; $date_of_msg = $row['post_details']; $thoughts_by = $row['added_by']; $attachent = $row['attachment']; $shared = $row['shared']; // for each post a user has made, a new div will be echo's echo " <div class='message_wrapper'> // all content here which displays the message and author. // consider this anchor link, and see $_GET approach below. <a href='/inc/del_post.php?id=$thought_id>'>Delete </a> <div id='toggleComment$thought_id' class='new_comment'> <form action='' method='post' enctype='multipart/form-data'> <table> <tr> <td> <textarea id='txtarea' name='comment_msg' cols='80' maxlength='180' placeholder=' add your comment...'></textarea> </td> <td> <input id='send' type='submit' name='send_comm' value='Share'/> </td> </tr> </table> </form> </div> </div>"; } // while loop closed // sending comments to database $comment = htmlentities(trim(strip_tags(@$_POST['comment_msg']))); $comment = mysqli_real_escape_string($connect, $comment); // if button is pressed, do this... if(isset($_POST['send_comm'])){ if (!empty ($comment)){ $insert_comment = mysqli_query ($connect, "INSERT INTO user_comments VALUES ('','$comment','$username','$user','0','$thought_id')"); header ("Location: /profile_page/$user"); } } ?>