[SOLVED] Creating an edit profile page.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CraniumDesigns
Forum Newbie
Posts: 18
Joined: Fri Nov 07, 2003 1:35 am

[SOLVED] Creating an edit profile page.

Post by CraniumDesigns »

here's my code. i want it to display a form with all the values of each field set to the values alreayd in the database, then have them be able to change it, and have it post to itself and update the database. so far i am only trying it with one variable, first_name, but my code isn't updating the database. it just reloads the page. here's my code:

Code: Select all

<?php

session_start();  
header("Cache-control: private"); // IE 6 Fix

include 'header.inc';

//update fields posted by form below
if($updated) {
	mysql_query("UPDATE users SET first_name=$user[first_name] WHERE username='". $_SESSION['my_username'] ."'");
}

echo "<p align='center'><a href='profile.php?username=". $_SESSION['my_username'] ."'>My Profile</a></p>";

$result = @mysql_query("SELECT * FROM users WHERE username='". $_SESSION['my_username']. "'");

while($user = mysql_fetch_array($result)) {

	echo ("
		<form action='edit_profile.php' method='post'>
		<b>Name:</b> <input type='text' name='first_name' value='$user[first_name]'><br />
		<b>Email:</b> <input type='text' name='email_address' value='$user[email_address]'><br />
		<input type='hidden' name='updated' value='yes'>
		<input type='submit' name='Submit' value='Submit'>
		</form>
	");

		

}

echo "<p align=center><a href=logout.php>Logout</a></p>";

include 'footer.inc';

?>
?>[/quote]
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

You need the following:

1. if(isset($_POST['updated'])) instead of if($updated)

Info link: click

2. Your inserting an undefined variable into the database. $user[firstname] will be the value of the form's $_POST variable. What will be the name of the variable is $_POST['firstname']. So insert that within the DB instead of the current one. Same goes for the email address.

3. To trim your data before it's sent to the database using [php_man]trim[/php_man]()
Last edited by m3mn0n on Wed Jan 07, 2004 4:16 am, edited 1 time in total.
CraniumDesigns
Forum Newbie
Posts: 18
Joined: Fri Nov 07, 2003 1:35 am

Post by CraniumDesigns »

ok. i got that, except the trim() part. what is the point of that and what does it do?
CraniumDesigns
Forum Newbie
Posts: 18
Joined: Fri Nov 07, 2003 1:35 am

Post by CraniumDesigns »

ok, now i've got this:

Code: Select all

<?php

session_start();  
header("Cache-control: private"); // IE 6 Fix

include 'header.inc';

//update fields posted by form below
if(isset($_POST&#1111;'updated'])) &#123;
	mysql_query("UPDATE users SET first_name=($_POST&#1111;'first_name']) WHERE username='". $_SESSION&#1111;'my_username'] ."'");
&#125;

echo "<p align='center'><a href='profile.php?username=". $_SESSION&#1111;'my_username'] ."'>My Profile</a></p>";

$result = @mysql_query("SELECT * FROM users WHERE username='". $_SESSION&#1111;'my_username']. "'");

while($user = mysql_fetch_array($result)) &#123;

	echo ("
		<form action='edit_profile.php' method='post'>
		<b>Name:</b> <input type='text' name='first_name' value='$user&#1111;first_name]'><br />
		<b>Email:</b> <input type='text' name='email_address' value='$user&#1111;email_address]'><br />
		<input type='hidden' name='updated' value='yes'>
		<input type='submit' name='Submit' value='Submit'>
		</form>
	");

&#125;

echo "<p align=center><a href=logout.php>Logout</a></p>";

include 'footer.inc';

?>
but i get this error:

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in edit_profile.php on line 10
CraniumDesigns
Forum Newbie
Posts: 18
Joined: Fri Nov 07, 2003 1:35 am

Post by CraniumDesigns »

also, how do i have it update more than one variable, like the email address in the mysql_query command up top there?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Click that link. Enjoy. :)
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Try:

Code: Select all

<?php
//update fields posted by form below 
if(isset($_POST['updated'])) { 
$first_name = trim($_POST['first_name']);
   mysql_query("UPDATE users SET first_name='".$first_name."' WHERE username='". $_SESSION['my_username'] ."'"); 
}
?>
Updating: http://www.mysql.com/doc/en/UPDATE.html
CraniumDesigns
Forum Newbie
Posts: 18
Joined: Fri Nov 07, 2003 1:35 am

Post by CraniumDesigns »

nice! that worked. i guess i had to separate the variable out of the query with the periods. now could someone tell me how to set multiple variables within the query and tell me what the purpose of the trim function is. i know it trims whitespace, is that like if he user puts in extra spaces on either side of the input?
CraniumDesigns
Forum Newbie
Posts: 18
Joined: Fri Nov 07, 2003 1:35 am

Post by CraniumDesigns »

ok, if gured out out how to multiples, just by spearating with spaces. nevermind about any of it. i think i understand it. thanks sami.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

No problemo, Steve. =)
Post Reply