[英]How to Display Current Username In Form Field then Input from the Form into Another Table - “Forum Posts”
I am designing a simple forum. 我正在设计一个简单的论坛。 On the page for adding a topic, I found a way to automatically insert the active username where the user would otherwise type it in. The code I got from the textbook (Teach Yourself PHP, MySQL and Apache All in One, 5th Edition) originally had the user typing in their email address there. 在添加主题的页面上,我找到了一种自动在用户要键入的位置插入活动用户名的方法。最初从教科书(自学PHP,MySQL和Apache All in One,第5版)中获得的代码让用户在那里输入他们的电子邮件地址。 There's an error that causes a redirect from the page/code that processes the form. 发生错误,导致从处理表单的页面/代码重定向。 I am sure it is because that username cannot be inserted into the table at the database where the email address would have gone. 我敢肯定这是因为用户名无法插入电子邮件地址所在的数据库的表中。 The column ("post_owner") in the table is just varchar (150). 表中的列(“ post_owner”)只是varchar(150)。 Perhaps using "echo" in the form merely displays but adds no useable value to be processed?? 也许在表单中使用“ echo”仅显示但不添加任何有用的值进行处理?
I am trying to find a way to display the current username and have it go to "post_owner" in the database table. 我试图找到一种方法来显示当前用户名,并将其转到数据库表中的“ post_owner”。 Can anyone show me how to display the active username and have it go to the relevant column (post_owner) in the database table? 谁能告诉我如何显示活动的用户名并将其转到数据库表中的相关列(post_owner)? I will paste in the form, processing page, and table. 我将粘贴表格,处理页面和表格。 Thanks. 谢谢。
The form, (addtopic.php) 表单,(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>
The processing page (do_addtopic.php) 处理页面(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>
These are the two tables: 这是两个表:
`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
)
Disabled input won't be submitted to server. 禁用的输入将不会提交到服务器。
Try to remove disabled="disabled"
from <input type="text" disabled="disabled" value=<?php echo $_SESSION['usr_id']; ?> name="topic_owner" size="30" maxlength="150" />
尝试从<input type="text" disabled="disabled" value=<?php echo $_SESSION['usr_id']; ?> name="topic_owner" size="30" maxlength="150" />
删除disabled="disabled"
<input type="text" disabled="disabled" value=<?php echo $_SESSION['usr_id']; ?> name="topic_owner" size="30" maxlength="150" />
<input type="text" disabled="disabled" value=<?php echo $_SESSION['usr_id']; ?> name="topic_owner" size="30" maxlength="150" />
If you want to prevent the user to change the value, use readonly
attribute instead. 如果要防止用户更改值,请改用readonly
属性。
See http://www.w3schools.com/TAGs/att_input_readonly.asp 参见http://www.w3schools.com/TAGs/att_input_readonly.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.