gpt4 book ai didi

php - 如何在表单字段中显示当前用户名,然后从表单输入另一个表 - "Forum Posts"

转载 作者:行者123 更新时间:2023-11-29 11:09:08 24 4
gpt4 key购买 nike

我正在设计一个简单的论坛。在添加主题的页面上,我找到了一种方法来自动插入事件用户名,否则用户会在其中输入用户名。我最初从教科书(Teach Yourself PHP, MySQL and Apache All in One,第五版)中获得的代码让用户在那里输入他们的电子邮件地址。存在导致处理表单的页面/代码重定向的错误。我确信这是因为该用户名无法插入到电子邮件地址所在的数据库表中。表中的列(“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
)

最佳答案

禁用的输入不会提交到服务器。

尝试删除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

关于php - 如何在表单字段中显示当前用户名,然后从表单输入另一个表 - "Forum Posts",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40926780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com