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
CraniumDesigns
Forum Newbie
Posts: 18 Joined: Fri Nov 07, 2003 1:35 am
Post
by CraniumDesigns » Wed Jan 07, 2004 1:56 am
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]
m3mn0n
PHP Evangelist
Posts: 3548 Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada
Post
by m3mn0n » Wed Jan 07, 2004 4:03 am
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 » Wed Jan 07, 2004 4:10 am
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 » Wed Jan 07, 2004 4:16 am
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ї'updated'])) {
mysql_query("UPDATE users SET first_name=($_POSTї'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';
?>
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 » Wed Jan 07, 2004 4:17 am
also, how do i have it update more than one variable, like the email address in the mysql_query command up top there?
m3mn0n
PHP Evangelist
Posts: 3548 Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada
Post
by m3mn0n » Wed Jan 07, 2004 4:17 am
Click that link. Enjoy.
m3mn0n
PHP Evangelist
Posts: 3548 Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada
Post
by m3mn0n » Wed Jan 07, 2004 4:20 am
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 » Wed Jan 07, 2004 4:26 am
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 » Wed Jan 07, 2004 4:28 am
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.
m3mn0n
PHP Evangelist
Posts: 3548 Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada
Post
by m3mn0n » Wed Jan 07, 2004 4:30 am
No problemo, Steve. =)