首次发送邮件后,将用户重定向到会话?

时间:2014-04-18 21:46:24

标签: php html mysql

我正在使用以下代码让用户使用PHP和MySQL在我的网站上创建一个新的对话(我知道它过时但在网站完成之前不会更新。)我试图将用户重定向到对话发送了第一条消息......

<form action="" id="newConvo" method="POST">
            <div class="row">
                <div class="form-group">
                    <div class="col-md-6">
                        <label>To: *</label>
                        <input type="text" value="" list="usernamelist" data-msg-required="Please enter a members username." maxlength="100" class="form-control" name="to" id="to" required>
                        <datalist id="usernamelist">
            <? print "$searchlist"; ?>
            </datalist>
                    </div>
                    <div class="col-md-6">
                        <label>Subject: *</label>
                        <input type="text" value="<?php if (isset($_POST['subject'])) echo htmlentities($_POST['subject']); ?>" data-msg-required="Please enter a conversation subject." maxlength="100" class="form-control" name="subject" id="subject" required>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="form-group">
                    <div class="col-md-12">
                        <label>Your message: *</label>
                        <textarea id="body2" rows="7" name="body2" data-msg-required="Please enter a valid message." class="form-control" required><?php if (isset($_POST['body2'])) echo htmlentities($_POST['body2']); ?></textarea>

                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                <input type="hidden" name="from" value="<?php echo $user_data['username']; ?>">
                    <input type="submit" name="Submit" value="Send Message" class="btn btn-lg btn-primary">
                </div>
            </div>
        </form>

function create_conversation($user_ids, $subject, $body2, $to, $from){
    $subject    = mysql_real_escape_string(htmlentities($subject));
    $body       = mysql_real_escape_string(htmlentities($body2));
    $to         = mysql_real_escape_string(htmlentities($to));
    $from       = mysql_real_escape_string(htmlentities($from));


    mysql_query("INSERT INTO `conversations` (`conversation_subject`, `sender`, `recipient`) VALUES ('{$subject}', '{$from}', '{$to}')");



    $conversation_id = mysql_insert_id();

    $sql = "INSERT INTO `conversations_messages` (`conversation_id`, `userid`, `message_date`, `message_text`)
            VALUES ({$conversation_id}, {$_SESSION['userid']}, UNIX_TIMESTAMP(), '{$body}')";

    mysql_query($sql);

    $values = array("({$conversation_id}, {$_SESSION['userid']}, UNIX_TIMESTAMP(), 0)");

    foreach ($user_ids as $userid){
        $userid = (int) $userid;
        $values[] = "({$conversation_id}, {$userid}, 0, 0)";
    }

    $sql = "INSERT INTO `conversations_members` (`conversation_id`, `userid`, `conversation_last_view`, `conversation_deleted`)
            VALUES " . implode(", ", $values);

    mysql_query($sql);
}

我会在函数的底部添加一些东西,也许是一个标题来重定向它们,例如:

header( 'Location: messages.php?page=3&conversation_id=$conversation_id' ) ;

或类似的东西?

2 个答案:

答案 0 :(得分:0)

你的想法基本上是正确的,但请记住,只有在没有写入输出的情况下才能设置任何标题。 确保在任何其他html代码出现之前,将调用带有此header命令的函数。

此外,如果您希望标题内容正常工作,请使用双引号

header("Location: messages.php?page=3&conversation_id=$conversation_id");

header('Location: messages.php?page=3&conversation_id='.$conversation_id);

答案 1 :(得分:0)

根据您的评论,看起来问题就是您的单引号。 PHP将单引号内的字符串视为写入的字符串(无变量替换),但在双引号内执行变量替换。尝试:

header( "Location: messages.php?page=3&conversation_id=$conversation_id" ) ;

(假设$conversation_id的值已经正确)