显示来自 MySQL 数据库的用户信息的个人资料页面

Profile page that displays user information from MySQL database

希望有人能帮上忙。我正在尝试从我的 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>

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

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

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

将发送给 php 的字段,您可以像这样给每个字段命名:

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

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

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

在 php 文件中:

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

这里是您表单的所有字段。

希望对您有所帮助。

首先我建议使用 PDO ( http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers )

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

但是如果您正在学习 PHP 并且不想从框架开始,我建议您使用对象功能并将您的应用程序分成文件。

您可以创建的第一个文件将是用户模型 class select 并更新用户详细信息此 class 应该使用 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);
}

请记住,让应用程序变得更好的安全方法是使用框架,否则在从用户那里获取数据时必须非常小心 但我认为这可以帮助您理解 PHP 结构

然后在表格中:

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