首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从PHP表单创建具有用户凭据的页面

问从PHP表单创建具有用户凭据的页面
EN

Stack Overflow用户
提问于 2015-07-05 08:30:46
回答 2查看 482关注 0票数 1

我想用php创建一个页面,并输入表单中的文本。

我知道的是:

  • 我知道一些php以及如何将内容写入数据库。
  • 我知道如何在html中创建表单。
  • 我需要使用像createUserProfiles.php这样的模板将文本从表单/u/username.php发送到/u/username.php。

边注:,我知道如何创建页面,但我不知道如何在其中编写php代码,也不知道如何使用模板创建用户配置文件。我试着在谷歌上搜索,但没有找到任何答案。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-05 09:32:18

createUserProfiles.php

代码语言:javascript
复制
<form method="post" action="u/username.php">
<input type="text" name="username">
<input type="text" name="email">
<input type="submit" name="submit" value="Create User">
</form>

现在,我们将将数据提交到数据库,并通过在数据库中加载模板u/username.php将这些提交的值呈现给templates/profile-templates.php。

代码语言:javascript
复制
<?php
//connect to db
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
//feed the values from previous form to db
$stmt = $db->prepare("INSERT INTO table(username,email) VALUES(:uname,:email)");
$stmt->execute(array(':uname' => $_POST['username'], ':email' => $_POST['email']));
//now make an array with key name and its values
$attribute = array ('{user_name}'=>$_POST['username'], '{email}'=>$_POST['email']);
//load the template into a variable called $html
$html = file_get_contents ('templates/profile-templates.php');
//Now replace {user_name} and {email} present in template file with real data fetched from user.
foreach($attribute as $key => $value){
$html = str_replace ("{$key}", $value, $html);}
//At last echo the variable
echo $html;

只需确保模板文件中有花括号,其中数据将通过PHP放置。参考文献

票数 1
EN

Stack Overflow用户

发布于 2015-07-05 09:15:38

您的方法很难使用,您应该使用sql服务器,因为数据类型和存储功能都很容易使用。遵循howCodes关于创建社交网络的教程,它将为您提供html表单和php的基本概念。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31228511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档