简体   繁体   English

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

[英]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.

相关问题 在输入表单中显示来自其他表的字段 - Display field from different table in a input form 当用户从yii2的下拉列表中选择名称时,如何获取并在另一个表单字段中显示用户名? - how to get and display username in another form field when user select name from dropdownlist in yii2? 如何根据用户从表单输入的数据,在另一个表的mysql中插入数据字段? - How to insert a field of data in mysql from another table, based from user input from a form? 如何在不使用SMF登录表单的情况下从另一表单登录SMF论坛 - How to login into SMF forum from another form without use SMF login form 如何在一种形式(提交后)中将值(在文本字段中)显示为另一种形式(两者都在同一页面上) - how to display a value(in text field) from one form(after submitting it) to another form (both are on the same page) 如何将输入字段值从一个表单传递到 Woocommerce 不同页面中的另一个表单 - How to Pass a input field value from one form to another form in the different page in Woocommerce 如何在论坛上检索和显示帖子? - How does one retrieve and display posts on a forum? PHP-如何在表单字段中显示当前选定的值 - PHP-How to Display Current Selected Value in Form field 如何在输入表单字段中插入当前日期作为值 - How can i insert current date as a value in an input form field 显示从数据库到cakephp的当前导入文件(html表形式) - Display current import file (html table form) from database into cakephp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM