会话没有通过AJAX正确发送


Session not sending correctly through AJAX

我有以下代码,我认为工作正常,但结果是用户会话没有正确发送。假设我想发一个帖子,它不取我的id,它取最后一个注册我网站的用户的id。为什么会这样?

我有这个作为我的$userid变量,它应该占用我的会话。我正在初始化页面顶部的会话。

我做错了什么?

$(document).ready(function(){ 
             $("#submit_announcement").on("click", function () {
             var user_message = $("#announcement_message").val();
                //$user = this.value;
                 $user = $("#approved_id").val();
                $.ajax({ 
                    url: "insert_announcements.php", 
                    type: "POST",
                    data: {
                           "user_id": $user,
                                        //"message": user_message
                                        "user_message": user_message
                            },
                    success: function (data) {
                           //  console.log(data); // data object will return the response when status code is 200
                             if (data == "Error!") {
                                 alert("Unable to get user info!");
                                 alert(data);
                             } else {
                                 $(".announcement_success").fadeIn();
                                 $(".announcement_success").show();
                                 $('.announcement_success').html('Announcement Successfully Added!');
                                 $('.announcement_success').delay(5000).fadeOut(400);
                             }
                         },
                         error: function (xhr, textStatus, errorThrown) {
                             alert(textStatus + "|" + errorThrown);
                             //console.log("error"); //otherwise error if status code is other than 200.
                         }
                     });
                 });
             });

PHP和Form

$userid = ( isset( $_SESSION['user'] ) ? $_SESSION['user'] : "" );
try {
    //Prepare
     $con = mysqli_connect("localhost", "", "", "");
     if ($user_stmt = $con->prepare("SELECT `id` FROM users")) {
        $user_stmt->execute();
        $user_stmt->bind_result($user_id); 
        if (!$user_stmt) {
            throw new Exception($con->error);
        }
     }
        $user_stmt->store_result();
         $user_result = array();
?>               
     <div class="announcement_success"></div>
            <p>Add New Announcement</p>
                <form action="" method="POST" id="insert_announcements">
                <input type="hidden" value="<?php echo $userid; ?>" id="approved_id" name="user_id" />
                    <textarea rows="4" cols="50" id="announcement_message" name="message" class="inputbarmessage" placeholder="Message" required></textarea>
                    <label for="contactButton">
                        <button type="button" class="contactButton" id="submit_announcement">Add Announcement</button>
                    </label>
                </form>

更新:PHP文件显示示例

// $announcement_user_id= $_POST['user_id'];
$userid = ( isset( $_SESSION['user'] ) ? $_SESSION['user'] : "" );
$announcement_message= $_POST['user_message'];
$test = print_r($_POST, true); 
file_put_contents('test.txt', $test); 
//var_dump($announcement_user_id);
$con = mysqli_connect("localhost", "", "", "");
$stmt2 = $con->prepare("INSERT INTO announcements (user_id, message, date) VALUES (?, ?, NOW())");
    if ( !$stmt2 || $con->error ) {
        // Check Errors for prepare
         die('Announcement INSERT prepare() failed: ' . htmlspecialchars($con->error));
    }
    if(!$stmt2->bind_param('is', $userid, $announcement_message)) {
        // Check errors for binding parameters
        die('Announcement INSERT bind_param() failed: ' . htmlspecialchars($stmt2->error));
    }
    if(!$stmt2->execute()) {
        die('Announcement INSERT execute() failed: ' . htmlspecialchars($stmt2->error));
    }
        //echo "Announcement was added successfully!";
    else
    {
         echo "Announcement Failed!";
    }

您正在选择所有用户:

SELECT `id` FROM users

所以当你从结果中得到一条记录时,它很可能恰好是表中最新的记录

你试图将参数绑定到i:

$user_stmt->bind_result($user_id);

所以也许你想有一个WHERE条款?

SELECT `id` FROM users WHERE `id` = ?

虽然,那似乎…不必要的。因为你已经的ID。您似乎是从客户端发布ID, 保持它在会话状态,从数据库中获取它。所以你在这里想要做什么并不是很清楚。但有一点很清楚,查询将返回该表中的每条记录