hi

can any one tell me how to make a profile page for my website i have been tring for a bit but just can not do it sort of new to php.
i have a dater base called clubwigan with a table in it called
wigan_users
in that table i have
the
id, username, password, email.
in the profile page i want the use to be able to change his/her username password and email adress
HOW DO I DO THIS????
thanks alot

Just like any other database access, build a form then process the form. While you're processing the form build a query to 'UPDATE' the user's information in your database table.

Ok well there are a couple of ways to go about this, but im gonna show you one.
1.

<?php
$con = mysql_connect("server","username","password");
if (!$con) {
    die ('Could not connect!: ' . mysql_error());
}
mysql_select_db("db_name", $con);
$result = mysql_query("SELECT * FROM tablename"); <-- this is gonna be what ever you want. 
while ($row = mysql_fetch_array($result)) {
echo "<form action='update.php' method='post'>
<input type='text' name='id' value='$row[id]'><br> 
<input type='text' name='name' value='$row[name]'>... and so forth.
<input type='submit' name='submit'>
</form>";
}
mysql_close($con);
?>

2. update.php

$con = mysql_connect("server","username","password");
if (!$con) {
    die ('Could not connect!: ' . mysql_error());
}
mysql_select_db("db_name", $con);
mysql_query("UPDATE tablename SET name = '$_POST[name]' WHERE id = '$_POST[id]'");
?>

If you really want to learn, go to http://www.w3schools.com/. Im not gonna do the work for you, but ill just give you an idea.

thanks i will have a look at that script and post any problem i have

thanks alot being big help

ok ill give you a basic example and based in your inputs
1st create 3 files

connect.php

mysql_connect('localhost','root',''); //your connection
mysql_select_db('yourdatabase'); //your database

myprofile.php

<?php

include('connect.php');

$sql = mysql_query( "SELECT * FROM user WHERE id='1'" );  //this id is just a sample, it should be a session variable. you must  store your id through session. so that your id can propagate through out the page.

echo "<h2>Profile</h2>
<form method='post' action='editprofile.php'>
<table>";

$row=mysql_fetch_array($sql);
echo "<tr><th>Name: </th><td>".$row['username']."</td></tr>
	<tr><th>Password: </th><td><input type='password' value='".$row['password']."' disabled='true' /></td></tr>
	<tr><th>Email: </th><td>".$row['email']."</td></tr>";


echo "</table><br />
<input type='submit' value='edit profile' />
</form>";

?>

editprofile.php

<?php

include('connect.php');


if(isset($_POST['btnedit'])){
$user = $_POST['user'];
$pass = $_POST['pass'];
$email = $_POST['email'];

$sql = mysql_query( "UPDATE user SET username='".$user."', password='".$pass."', email='".$email."' WHERE id='1'" ); //again use session to this.

if($sql){
echo "<script>alert('profile updated');window.location='myprofle.php'</script>";
}else{
echo "<script>alert('updating profile failed!');</script>";
}

}




$sql = mysql_query( "SELECT * FROM user WHERE id='1'" );
$row = mysql_fetch_array($sql);
echo "<h2>Edit profile</h2>
<form method='post'>
<table>
<tr><th>username:</th><td><input type='text' name='user' value='".$row['username']."'/></td></tr>
<tr><th>password:</th><td><input type='password' name='pass' value='".$row['password']."'/></td></tr>
<tr><th>email:</th><td><input type='text' name='email' value='".$row['email']."'/></td></tr>
</table><br />
<input type='submit' name='btnedit' value='update' />
</form>";



?>

tried and test... its still incomplete and you should make some validations upon updating. make sure the new username doesn't match existing username and validate your email. You must do the rest. cause i feel like im doing your assignment here. By the way this is just a basic example at least im showing you the code rather than just explaining it to you textually.

ok ill give you a basic example and based in your inputs
1st create 3 files

connect.php

mysql_connect('localhost','root',''); //your connection
mysql_select_db('yourdatabase'); //your database

myprofile.php

<?php

include('connect.php');

$sql = mysql_query( "SELECT * FROM user WHERE id='1'" );  //this id is just a sample, it should be a session variable. you must  store your id through session. so that your id can propagate through out the page.

echo "<h2>Profile</h2>
<form method='post' action='editprofile.php'>
<table>";

$row=mysql_fetch_array($sql);
echo "<tr><th>Name: </th><td>".$row['username']."</td></tr>
	<tr><th>Password: </th><td><input type='password' value='".$row['password']."' disabled='true' /></td></tr>
	<tr><th>Email: </th><td>".$row['email']."</td></tr>";


echo "</table><br />
<input type='submit' value='edit profile' />
</form>";

?>

editprofile.php

<?php

include('connect.php');


if(isset($_POST['btnedit'])){
$user = $_POST['user'];
$pass = $_POST['pass'];
$email = $_POST['email'];

$sql = mysql_query( "UPDATE user SET username='".$user."', password='".$pass."', email='".$email."' WHERE id='1'" ); //again use session to this.

if($sql){
echo "<script>alert('profile updated');window.location='myprofle.php'</script>";
}else{
echo "<script>alert('updating profile failed!');</script>";
}

}




$sql = mysql_query( "SELECT * FROM user WHERE id='1'" );
$row = mysql_fetch_array($sql);
echo "<h2>Edit profile</h2>
<form method='post'>
<table>
<tr><th>username:</th><td><input type='text' name='user' value='".$row['username']."'/></td></tr>
<tr><th>password:</th><td><input type='password' name='pass' value='".$row['password']."'/></td></tr>
<tr><th>email:</th><td><input type='text' name='email' value='".$row['email']."'/></td></tr>
</table><br />
<input type='submit' name='btnedit' value='update' />
</form>";



?>

tried and test... its still incomplete and you should make some validations upon updating. make sure the new username doesn't match existing username and validate your email. You must do the rest. cause i feel like im doing your assignment here. By the way this is just a basic example at least im showing you the code rather than just explaining it to you textually.

thanks for this i will have a good look at it to morrow as i am getting error now but i thing that is just because i am geting tried will post results tomorrow.
thanks alot again

got it to work but still learning can you tell me what to reasearch to ba able to do
session variable

"//this id is just a sample, it should be a session variable. you must store your id through session. so that your id can propagate through out the page."

what should i reasearch to learn how to do this thanks alot

Clicky.

This should give you a decent start. Otherwise look up sessions in the online documentation on php.net.

on w3schools.com... they had simple example. for easy understanding... just follow fbody's link.

on w3schools.com... they had simple example. for easy understanding... just follow fbody's link.

sorry

i have read about sessions here http://www.w3schools.com/php/php_sessions.asp
and load of other places but i just don't know how to store your id through session. so that your id can propagate through out the page.
this project i am doing is to help me learn i am making it as i learn is is no assignment.
the way i have been making it is though watch tutorials and looking at script and make my own base on that current script.
but i got stuck on profile page and that is why i made this form.
can you help me a bit more please as i said i don't know what to do from here. i am suck:'(

thanks alot

storing id through session is very simple. here ill give you a basic example.

<?php

session_start() // you need to inorder for the session to work, this is very important. Or you can set autostart in the php.ini

$_SESSION['id'] = 1; // this is how you set session variable

//so that means that you had now created a session variable named $_SESSION['id'] and store your id, in my example i use 1;

//then you can get that value like this

$id = $_SESSION['id'];

//means you get your id through out the page... where you can use it on   major query on selecting specific rows.


?>

storing id through session is very simple. here ill give you a basic example.

<?php

session_start() // you need to inorder for the session to work, this is very important. Or you can set autostart in the php.ini

$_SESSION['id'] = 1; // this is how you set session variable

//so that means that you had now created a session variable named $_SESSION['id'] and store your id, in my example i use 1;

//then you can get that value like this

$id = $_SESSION['id'];

//means you get your id through out the page... where you can use it on   major query on selecting specific rows.


?>

thanks again!!! for this
but
how am i going to uses the sessions to grab the uses that is logged in so he/she and only edit there profile?

i dont know if you understand me right but it's pretty obvious... session works like global variable. So store the user id through session, through that id the user profile. ok ill provide add some basic login and ill revised all the code. basically im providing you all the code from log-in and viewing profile to editing profile.

connect.php

mysql_connect('localhost','root',''); //your connection
mysql_select_db('yourdatabase'); //your database

index.php

<?php

	include("connect.php");


if( isset($_POST['btnlogin']) ){
	
	$user = $_POST['user'];
	$pass = $_POST['pass'];

	if( $user ){

	$sql = mysql_query( "SELECT * FROM user WHERE username='$user'" );
		if( mysql_num_rows($sql) != 0 ){

				while($row=mysql_fetch_array($sql)){
					if($row['password'] == $pass){
					$_SESSION['id'] = $row['id']; // this is here use store the user id. $_SESSION['id'] is available globally
					echo "<script>window.location='myprofile.php';</script>"; // redirect
					}else{
					echo "<script>alert('Incorrect password!');</script>";
					}
				}
				
			}else{
		echo "<script>alert('username does not exist, register first!');</script>";
		}

}

}
	
?> 



<form method='post'>
<h2>Log-in</h2>
<table>
<tr><td>Username :</td><td> <input name="user" type="text" /></td></tr>
<tr><td>Password :</td><td> <input name="pass" type="password" /></td></tr>
</table>
<input type="submit" name="btnlogin" value='log-on' />
</form>

myprofile.php

<?php

include('connect.php');

$sql = mysql_query( "SELECT * FROM user WHERE id='".$_SESSION['id']."'" );  // call the $_SESSION['id'] which stores the user id, we've set from log-in

echo "<h2>Profile</h2>
<form method='post' action='editprofile.php'>
<table>";

$row=mysql_fetch_array($sql);
echo "<tr><th>Name: </th><td>".$row['username']."</td></tr>
	<tr><th>Password: </th><td><input type='password' value='".$row['password']."' disabled='true' /></td></tr>
	<tr><th>Email: </th><td>".$row['email']."</td></tr>";


echo "</table><br />
<input type='submit' value='edit profile' />
</form>";

?>

editprofile.php

<?php

include('connect.php');


if(isset($_POST['btnedit'])){
$user = $_POST['user'];
$pass = $_POST['pass'];
$email = $_POST['email'];

$sql = mysql_query( "UPDATE user SET username='".$user."', password='".$pass."', email='".$email."' WHERE id='".$_SESSION['id']."'" ); // call $_SESSION['id'], which holds the user id

if($sql){
echo "<script>alert('profile updated');window.location='myprofle.php'</script>";
}else{
echo "<script>alert('updating profile failed!');</script>";
}

}




$sql = mysql_query( "SELECT * FROM user WHERE id='".$_SESSION['id']."'" ); // call $_SESSION['id'], which holds the user id
$row = mysql_fetch_array($sql);
echo "<h2>Edit profile</h2>
<form method='post'>
<table>
<tr><th>username:</th><td><input type='text' name='user' value='".$row['username']."'/></td></tr>
<tr><th>password:</th><td><input type='password' name='pass' value='".$row['password']."'/></td></tr>
<tr><th>email:</th><td><input type='text' name='email' value='".$row['email']."'/></td></tr>
</table><br />
<input type='submit' name='btnedit' value='update' />
</form>";



?>

now i give you all the help you can get... i dont know if you still dont understand this... im like spoon feeding here. I provided you all the code and the explanation. and this is tried and tested. Please try to run these code. as i patiently code this for you.

i dont know if you understand me right but it's pretty obvious... session works like global variable. So store the user id through session, through that id the user profile. ok ill provide add some basic login and ill revised all the code. basically im providing you all the code from log-in and viewing profile to editing profile.

connect.php

mysql_connect('localhost','root',''); //your connection
mysql_select_db('yourdatabase'); //your database

index.php

<?php

	include("connect.php");


if( isset($_POST['btnlogin']) ){
	
	$user = $_POST['user'];
	$pass = $_POST['pass'];

	if( $user ){

	$sql = mysql_query( "SELECT * FROM user WHERE username='$user'" );
		if( mysql_num_rows($sql) != 0 ){

				while($row=mysql_fetch_array($sql)){
					if($row['password'] == $pass){
					$_SESSION['id'] = $row['id']; // this is here use store the user id. $_SESSION['id'] is available globally
					echo "<script>window.location='myprofile.php';</script>"; // redirect
					}else{
					echo "<script>alert('Incorrect password!');</script>";
					}
				}
				
			}else{
		echo "<script>alert('username does not exist, register first!');</script>";
		}

}

}
	
?> 



<form method='post'>
<h2>Log-in</h2>
<table>
<tr><td>Username :</td><td> <input name="user" type="text" /></td></tr>
<tr><td>Password :</td><td> <input name="pass" type="password" /></td></tr>
</table>
<input type="submit" name="btnlogin" value='log-on' />
</form>

myprofile.php

<?php

include('connect.php');

$sql = mysql_query( "SELECT * FROM user WHERE id='".$_SESSION['id']."'" );  // call the $_SESSION['id'] which stores the user id, we've set from log-in

echo "<h2>Profile</h2>
<form method='post' action='editprofile.php'>
<table>";

$row=mysql_fetch_array($sql);
echo "<tr><th>Name: </th><td>".$row['username']."</td></tr>
	<tr><th>Password: </th><td><input type='password' value='".$row['password']."' disabled='true' /></td></tr>
	<tr><th>Email: </th><td>".$row['email']."</td></tr>";


echo "</table><br />
<input type='submit' value='edit profile' />
</form>";

?>

editprofile.php

<?php

include('connect.php');


if(isset($_POST['btnedit'])){
$user = $_POST['user'];
$pass = $_POST['pass'];
$email = $_POST['email'];

$sql = mysql_query( "UPDATE user SET username='".$user."', password='".$pass."', email='".$email."' WHERE id='".$_SESSION['id']."'" ); // call $_SESSION['id'], which holds the user id

if($sql){
echo "<script>alert('profile updated');window.location='myprofle.php'</script>";
}else{
echo "<script>alert('updating profile failed!');</script>";
}

}




$sql = mysql_query( "SELECT * FROM user WHERE id='".$_SESSION['id']."'" ); // call $_SESSION['id'], which holds the user id
$row = mysql_fetch_array($sql);
echo "<h2>Edit profile</h2>
<form method='post'>
<table>
<tr><th>username:</th><td><input type='text' name='user' value='".$row['username']."'/></td></tr>
<tr><th>password:</th><td><input type='password' name='pass' value='".$row['password']."'/></td></tr>
<tr><th>email:</th><td><input type='text' name='email' value='".$row['email']."'/></td></tr>
</table><br />
<input type='submit' name='btnedit' value='update' />
</form>";



?>

now i give you all the help you can get... i dont know if you still dont understand this... im like spoon feeding here. I provided you all the code and the explanation. and this is tried and tested. Please try to run these code. as i patiently code this for you.

thanks for this is see what you mean now how i was suppost to do it but when i login in and get directed to the profile page i get an error it says

Notice: Undefined variable: _SESSION in C:\wamp\www\profile\myprofile.php on line 5

screen shot of proble below

thanks for this is see what you mean now how i was suppost to do it but when i login in and get directed to the profile page i get an error it says

Notice: Undefined variable: _SESSION in C:\wamp\www\profile\myprofile.php on line 5

screen shot of proble below

oh sorry dude... i forgot to update the connect.php file. add the session_start();

connect.php

session_start(); //start session
mysql_connect('localhost','root',''); //your connection
mysql_select_db('yourdatabase'); //your database

It's more important that session_start() be at the top of "myprofile.php". That's where the error is. If you include "connect.php" after the session starts, then it's not necessary to include it in that file. This also makes the connect file more portable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.