Page 1 of 1

[SOLVED] Creating an edit profile page.

Posted: Wed Jan 07, 2004 1:56 am
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]

Posted: Wed Jan 07, 2004 4:03 am
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]()

Posted: Wed Jan 07, 2004 4:10 am
by CraniumDesigns
ok. i got that, except the trim() part. what is the point of that and what does it do?

Posted: Wed Jan 07, 2004 4:16 am
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

Posted: Wed Jan 07, 2004 4:17 am
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?

Posted: Wed Jan 07, 2004 4:17 am
by m3mn0n
Click that link. Enjoy. :)

Posted: Wed Jan 07, 2004 4:20 am
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

Posted: Wed Jan 07, 2004 4:26 am
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?

Posted: Wed Jan 07, 2004 4:28 am
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.

Posted: Wed Jan 07, 2004 4:30 am
by m3mn0n
No problemo, Steve. =)