从MySQL数据库显示用户信息的配置文件页面

时间:2016-03-25 03:19:16

标签: php html mysql twitter-bootstrap user-profile

我希望有人可以提供帮助。我试图从我的SQL获取登录用户信息以显示在配置文件页面上。然后,该用户可以更改字段中的任何内容并保存以更新数据库。

到目前为止,这是我的个人资料页面,我只是不确定如何将php实现到表单中,因此为什么名称php脚本位于顶部。

我对这一切都不熟悉,现在已经在这里搜索了一天,但找不到答案或是我理解的东西。任何帮助都会非常感激。

<?

session_start();

include("connection.php");

$query="SELECT name FROM users WHERE id='".$_SESSION['id']."' LIMIT 1";

$result = mysqli_query($link,$query);

$row = mysqli_fetch_array($result);

$diary=$row['name'];

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Profile</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href='https://fonts.googleapis.com /css?family=Lato:400,300,100,300italic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="resources/css/profilestyles.css">
</head>

<body data-spy="scroll" data-target=".navbar-collapse">
<div class="navbar navbar-default navbar-fixed-top">
  <div class="container">
    <div class="navbar-header">
      <button class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button>
      <a class="navbar-brand">Profile</a> </div>
    <div class="collapse navbar-collapse">
      <form class="navbar-form navbar-right" method="post">
        <ul class="nav nav-pills">
          <li role="presentation"><a href="mainpage.php">My connections</a></li>
          <li role="presentation"><a href="#">World connections</a></li>
          <li role="presentation"  class="active"><a href="profile.php">Profile</a></li>
          <li role="presentation"><a href="#">Messages</a></li>
          <li role="presentation"><a href="index.php?logout=1">Logout</a></li>
        </ul>
      </form>
    </div>
  </div>
</div>
<div class="container">
  <h1>Edit Profile</h1>
  <hr>
  <div class="row"> 
    <!-- left column -->
    <div class="col-md-3">
      <div class="text-center"> <img src="//placehold.it/100" class="avatar img-circle" alt="avatar">
        <h6>Upload a different photo...</h6>
        <input class="form-control" type="file">
      </div>
    </div>

    <!-- edit form column -->
    <div class="col-md-9 personal-info">
      <div class="alert alert-info alert-dismissable"> <a class="panel-close close" data-dismiss="alert">×</a> <i class="fa fa-coffee"></i> This is an <strong>.alert</strong>. Use this to show important messages to the user. </div>
      <h3>Personal info</h3>
      <form class="form-horizontal" role="form">
        <div class="form-group">
          <label class="col-lg-3 control-label name">name:</label>
          <div class="col-lg-8">
            <input class="form-control" value="" type="text">
          </div>
        </div>
        <div class="form-group">
          <label class="col-lg-3 control-label">Email:</label>
          <div class="col-lg-8">
            <input class="form-control" value="katie@katie.com" type="text">
          </div>
        </div>
        <div class="form-group">
          <label class="col-lg-3 control-label">DOB:</label>
          <div class="col-lg-8">
            <input class="form-control" value="yyyy-mm-dd" type="date">
          </div>
        </div>
        <div class="form-group">
          <label class="col-lg-3 control-label">Country</label>
          <div class="col-lg-8">
            <input class="form-control" value="America" type="text">
          </div>
        </div>
        <div class="form-group">
          <label class="col-md-3 control-label">Password:</label>
          <div class="col-md-8">
            <input class="form-control" value="password" placeholder="At least 8 characters and 1 cap letter" type="password">
          </div>
        </div>
        <div class="form-group">
          <label class="col-md-3 control-label">Confirm password:</label>
          <div class="col-md-8">
            <input class="form-control" value="password" placeholder="At least 8 characters and 1 cap letter" type="password">
          </div>
        </div>
        <div class="form-group">
          <label class="col-md-3 control-label"></label>
          <div class="col-md-8">
            <input class="btn btn-primary" value="Save Changes" type="button">
            <span></span>
            <input class="btn btn-default" value="Cancel" type="reset">
          </div>
        </div>
      </form>
    </div>
  </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<!-- Include all compiled plugins (below), or include individual files as needed --> 
<script src="js/bootstrap.min.js"></script>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

在表单标记中输入以下代码:

form action="edit_profile.php" method="post"

其中“edit_profile”是用于接收表单数据的php文件的名称,对于每个表单,您只需一个php文件,“方法发布”它表示如何将数据发送到php文件

将发送到php的字段,你给每个人写了一个名字:

input class="form-control" value="" type="text" **name="name"**

表单按钮是提交类型。像这样:

button **type="submit"** value="save changes"

在php文件中:

$name= $_POST['**name**'];

以及此表单的所有字段。

我希望它有所帮助。

答案 1 :(得分:0)

首先,我将使用PDO(http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

编写应用程序的最佳解决方案可能是使用一些简单的框架作为Yii2框架(http://www.yiiframework.com/wiki/?tag=yii2

但是如果您正在学习PHP并且不想从框架开始,那么我会使用对象功能并将应用程序划分为文件。

你可以创建的第一个文件是User模型类,它选择并更新这个类应该使用PDO对象的用户详细信息,所以我这样说:

class DB
{
    private static $singleton;

    public static function getInstance() {
        if(is_null(self::$singleton)){
            self::$singleton = new PDO('mysql:host=localhost;dbname=test;charset=utf8mb4', 'root', '');
        }
        return self::$singleton;
    }
}

class User 
{    
    private $id;

    private $name;

    private $surname;

    public static function find($id)
    {
        $stmt = DB::getInstance()->prepare("SELECT * FROM users WHERE id=?");
        $stmt->execute(array($id));
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

        if(empty($rows)) {
            return null;
        }

        $user = new self();
        $user->id = $rows[0]['id'];
        $user->name = $rows[0]['name'];
        $user->surname = $rows[0]['surname'];

        // And any other 

        return $user;
    }

    public function getId()
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }

    public function getSurname()
    {
        return $this->surname;
    }

    // And any other 

    public function update($params)
    {
        // Shoul also validate the data from user input before store into database 

        if(isset($params['name']))
        {
            $this->name = $params['name'];
        }

        if(isset($params['surname']))
        {
            $this->surname = $params['surname'];
        }

        // And any other 

        $stmt = DB::getInstance()->prepare("UPDATE users SET name = ?, surname = ? WHERE id = ?");
        return $stmt->execute(array($this->name, $this->surname, $this->id));
    }

}

$user = User::find(1);

$user->update(['name' => 'John']);

// or simply 

if($_POST) {
  $user->update($_POST);
}

请记住,使用app更好的方法是使用框架,或者在从用户那里获取数据时必须非常小心         但我认为这可以帮助您理解PHP结构

然后是表格:

<input name="surname" class="form-control" value="<?= ($user) ? $user->getSurname() : '' ;?>" type="text">