如何在表单字段中显示当前用户名,然后从表单输入另一个表 - "论坛帖子"

时间:2016-12-02 07:17:41

标签: php html mysql forms

我正在设计一个简单的论坛。在添加主题的页面上,我找到了一种方法来自动插入用户可以在其中输入的活动用户名。我从教科书(自学PHP,MySQL和Apache All in One,第5版)中获得的代码最初让用户在那里输入他们的电子邮件地址。有一个错误导致从处理表单的页面/代码重定向。我确信这是因为该用户名不能插入到电子邮件地址已经消失的数据库的表中。表中的列(" post_owner")只是varchar(150)。也许使用" echo"在表单中只显示但没有添加任何可用的值进行处理??

我正在尝试找到一种显示当前用户名的方法,并将其转到" post_owner"在数据库表中。任何人都可以告诉我如何显示活动用户名并将其转到数据库表中的相关列(post_owner)?我将粘贴表单,处理页面和表格。感谢。

表格,(addtopic.php)

<form method="post" action="do_addtopic.php">

    <p><label for="topic_owner"><n5 style="color: #ffe066; font-size: 14pt;">Your Username:<n5></label><br/>
    <input type="text" disabled="disabled" value=<?php echo $_SESSION['usr_id']; ?> name="topic_owner" size="30" maxlength="150" /></p>

    <p><label for="topic_title"><n5 style="color: #ffe066; font-size: 14pt;">Topic Title:<n5></label><br/>
    <input type="text" id="topic_title" name="topic_title" size=""
    maxlength="150" required="required" /></p>

    <p><label for="post_text"><n5 style="color: #ffe066; font-size: 14pt;">Post Text:<n5></label><br/>
    <textarea id="post_text" name="post_text" rows="" cols="" ></textarea></p>

    <button type="submit" name="submit" value="submit" class="button">Add Topic</button>
</form>

处理页面(do_addtopic.php)

<?php
include_once 'dbalt.php';
doDB();

//check for required fields from the form
if ((!$_POST['topic_owner']) || (!$_POST['topic_title']) ||
(!$_POST['post_text'])) {
header("Location: addtopic.html");
exit;
}

//create safe values for input into the database
$clean_topic_owner = mysqli_real_escape_string($mysqli,
$_POST['topic_owner']);
$clean_topic_title = mysqli_real_escape_string($mysqli,
$_POST['topic_title']);
$clean_post_text = mysqli_real_escape_string($mysqli,
$_POST['post_text']);

//create and issue the first query
$add_topic_sql = "INSERT INTO forum_topics
(topic_title, topic_create_time, topic_owner)
VALUES ('".$clean_topic_title ."', now(),
'".$clean_topic_owner."')";

$add_topic_res = mysqli_query($mysqli, $add_topic_sql)
or die(mysqli_error($mysqli));

//get the id of the last query
$topic_id = mysqli_insert_id($mysqli);

//create and issue the second query
$add_post_sql = "INSERT INTO forum_posts
(topic_id, post_text, post_create_time, post_owner)
VALUES ('".$topic_id."', '".$clean_post_text."',
now(), '".$clean_topic_owner."')";

$add_post_res = mysqli_query($con, $add_post_sql)
or die(mysqli_error($mysqli));
//close connection to MySQL
mysqli_close($mysqli);

//create nice message for user
$display_block = "<p>The <strong>".$_POST["topic_title"]."</strong>
topic has been created.</p>";
?>
<!DOCTYPE html>
<html>
<head>
<title>New Topic Added</title>
</head>
<body>
<h1>New Topic Added</h1>
<?php echo $display_block; ?>
<p>Click to <a href="topiclist.php">view</a> the topic listings.</p>
</body>
</html>

这是两个表:

`forum_posts` (
  `post_id` int(11) NOT NULL,
  `topic_id` int(11) NOT NULL,
  `post_text` text,
  `post_create_time` datetime DEFAULT NULL,
  `post_owner` varchar(150) DEFAULT NULL
)
`forumusers` (
  `id` int(12) NOT NULL,
  `name` varchar(50) NOT NULL,
  `user_name` varchar(15) NOT NULL,
  `email` varchar(50) NOT NULL,
  `password` varchar(32) NOT NULL
)

1 个答案:

答案 0 :(得分:1)

已禁用的输入无法提交给服务器。

尝试从disabled="disabled"

中删除<input type="text" disabled="disabled" value=<?php echo $_SESSION['usr_id']; ?> name="topic_owner" size="30" maxlength="150" />

如果您想阻止用户更改值,请改用readonly属性。

请参阅http://www.w3schools.com/TAGs/att_input_readonly.asp