Reputation: 49
I hope someone can be of help. I am trying to get a logged in users information from my sql to display on a profile page. And then for that user to be able to change anything in the fields and save it to update the database.
This is my profile page so far, I'm just not sure on how to implement the php into the form hence why the name php script is sitting at the top.
I am new to all this and have searched about on here for a day now but can't find the answer or be it something I understand. Any help would be really appreciated.
<?
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="[email protected]" 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>
Upvotes: 1
Views: 5868
Reputation: 26
in the form tag you enter the following code:
form action="edit_profile.php" method="post"
where "edit_profile" is the name of your php file to receive the form data, for each form you one php file, the "method post" It indicates how the data will be sent to the php file
the fields that will be sent to the php you put a name to each like this :
input class="form-control" value="" type="text" **name="name"**
the button of form is submit type. like this:
button **type="submit"** value="save changes"
in the php file:
$name= $_POST['**name**'];
and here all fields of your form.
I hope it helps.
Upvotes: 1
Reputation: 330
First of all I will sugest to use PDO ( http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers )
The best solution to write souch an applications could be to use some simply framework souch as Yii2 framework ( http://www.yiiframework.com/wiki/?tag=yii2 )
But If you are learning PHP and don't wanna start with a framework I sugest you to use object features and divide you application into files.
The first file that you could create will be the User model class that select and update the user details this class should use the PDO object so I sugest sth like this:
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);
}
And remember this is not safe method of making app better is to use framework or you must really be carefull when getting data from your users But I think that this could help you to understood the PHP structure
Then in form :
<input name="surname" class="form-control" value="<?= ($user) ? $user->getSurname() : '' ;?>" type="text">
Upvotes: 0