Forum Moderators: coopster

Message Too Old, No Replies

PHP - MYSQL - Updating Profile Page - Im Really Confused

Updating Information in a Database Using a form

         

del_boy

1:39 am on May 15, 2005 (gmt 0)

10+ Year Member



Heres the story:

Im working on a members area and this part is for the 'profiles' editing page, and i can't seem to be able to post the varialbles into the table when the clicks submit.

I have just got so confused that my code is all messed up and now my head is.

I am collecting the info by using a?username=*usernamehere* method, as you might guess i have only been doing this for a few weeks now.

So PLEASE could someone help me!

<table width="100%" align="center">
<form name="update" action="<?php echo $results;?>" method="post">
<?php
// Make a MySQL Connection
mysql_connect("localhost", "HotingName", "HostingPassword") or die(mysql_error());
mysql_select_db("MyDataBase") or die(mysql_error());

// selecting my table
$result = mysql_query("SELECT * FROM ALS_signup WHERE username = '$username'")
or die(mysql_error());

// This is showing the members pictue and there details in input boxes and this is where i am soooo confused

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

echo "<tr><Td align='center' colspan=2>";
echo "<a href='profilespics/";
echo $row['upload_picture'];
echo ".jpg' >";
echo "<img border=0 width='100' height='100' src='profilespics/";
echo $row['upload_picture'];
echo ".jpg' ></a><br>";
if ($row["userlevel"] < 0) {echo "Website Admin";}
echo "</td></tr>";

$username = $row['username'];
$id = $row['id'];
$mailadres = $row['mailadres'];
$signupdate = $row['signupdate'];
$lastlogintime = $row['lastlogin'];
$fullname = $row['fullname'];
$DOB = $row['DOB'];
$location = $row['location'];
$gender = $row['gender'];
$faveband = $row['faveband'];
$favetvshow = $row['favetvshow'];
$favegame = $row['favegame'];
$about_me = $row['about_me'];
$EDIT_fullname = @$HTTP_POST_VARS["Edit_fullname"];
$NewFullName = @$HTTP_GET_VARS["fullname"];
$EDIT_fullname2 = @$HTTP_GET_VARS["fullname"];

echo "<tr><td WIDTH=120 bgcolor='$bgcolor1'>UserName</td><td bgcolor='$bgcolor2'>$username";
echo "</td></tr><tr><td bgcolor='$bgcolor1'>User ID</td><td bgcolor='$bgcolor2'>$id";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>eMail Address</td><td bgcolor='$bgcolor2'>";

echo "<a href='mailto:$mailadres'> $mailadres </a>";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>Sign Up Date</td><td bgcolor='$bgcolor2'>
<input name='EDIT$signupdate' value='$signupdate'>
";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>Last Login Time</td><td bgcolor='$bgcolor2'>
<input name='EDIT$lastlogintime' value='$lastlogintime'>
";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>Full Name</td><td bgcolor='$bgcolor2'>
<input name='EDIT_fullname' value='$fullname'>
";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>D.O.B</td><td bgcolor='$bgcolor2'>
<input name='EDIT$DOB' value='$DOB'>
";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>location</td><td bgcolor='$bgcolor2'>
<input name='EDIT$location' value='$location'>
";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>M/F (Gender)</td><td bgcolor='$bgcolor2'>
<input name='EDIT$gender' value='$gender'>
";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>Fave Band</td><td bgcolor='$bgcolor2'>
<input name='EDIT$faveband' value='$faveband'>
";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>Fave TV Show</td><td bgcolor='$bgcolor2'>
<input name='EDIT$favetvshow' value='$favetvshow'>
";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>Fave Game</td><td bgcolor='$bgcolor2'>
<input name='EDIT$favegame' value='$favegame'>
";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>About Me</td><td bgcolor='$bgcolor2'>
<input name='EDIT$about_me' value='$about_me'>
";

echo "</td></tr></table>";
}
echo "</select></tr></td>";

?>
<input type="submit" value="Update Profile">
</form>

<?php
$SQL = "UPDATE ALS_signup SET fullname = '$fullname', location = '$location', gender = '$gender', faveband = '$faveband', favetvshow = '$favetvshow', favegame = '$favegame', about_me = '$about_me' WHERE username = '$username'";
$result = mysql_query($SQL) or die(mysql_error());
?>

Please help!

ramoneguru

6:59 am on May 15, 2005 (gmt 0)

10+ Year Member



<?php

/*
Take out my comments if they are getting in the way
Lookup how to make functions in php. It is on php.net
It looks like this:
function getForm()
{
do stuff in here
}//end getForm

I just made some minor changes but it looks like it should work
*/

echo '<table width="100%" align="center">';
echo '<form name="update" action="update.php" method="post">';

Make a MySQL Connection/mysql_connect("localhost", "HotingName", "HostingPassword") or die(mysql_error());
mysql_select_db("MyDataBase") or die(mysql_error());

selecting my table
$result = mysql_query("SELECT * FROM ALS_signup WHERE username = '$username'")or die(mysql_error());

// This is showing the members pictue and there details in input boxes and this is where i am soooo confused

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

echo "<tr><Td align='center' colspan=2>";
echo "<a href='profilespics/";
echo $row['upload_picture'];
echo ".jpg' >";
echo "<img border=0 width='100' height='100' src='profilespics/";
echo $row['upload_picture'];
echo ".jpg' ></a><br>";
if ($row["userlevel"] < 0) {echo "Website Admin";}
echo "</td></tr>";

$username = $row['username'];
$id = $row['id'];
$mailadres = $row['mailadres'];
$signupdate = $row['signupdate'];
$lastlogintime = $row['lastlogin'];
$fullname = $row['fullname'];
$DOB = $row['DOB'];
$location = $row['location'];
$gender = $row['gender'];
$faveband = $row['faveband'];
$favetvshow = $row['favetvshow'];
$favegame = $row['favegame'];
$about_me = $row['about_me'];
$EDIT_fullname = @$HTTP_POST_VARS["Edit_fullname"];
$NewFullName = @$HTTP_GET_VARS["fullname"];
$EDIT_fullname2 = @$HTTP_GET_VARS["fullname"];

echo "<tr><td WIDTH=120 bgcolor='$bgcolor1'>UserName</td><td bgcolor='$bgcolor2'>$username";
echo "</td></tr><tr><td bgcolor='$bgcolor1'>User ID</td><td bgcolor='$bgcolor2'>$id";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>eMail Address</td><td bgcolor='$bgcolor2'>";

echo "<a href='mailto:$mailadres'> $mailadres </a>";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>Sign Up Date</td><td bgcolor='$bgcolor2'>
<input name='EDIT$signupdate' value='$signupdate'>
";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>Last Login Time</td><td bgcolor='$bgcolor2'>
<input name='EDIT$lastlogintime' value='$lastlogintime'>
";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>Full Name</td><td bgcolor='$bgcolor2'>
<input name='EDIT_fullname' value='$fullname'>
";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>D.O.B</td><td bgcolor='$bgcolor2'>
<input name='EDIT$DOB' value='$DOB'>
";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>location</td><td bgcolor='$bgcolor2'>
<input name='EDIT$location' value='$location'>
";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>M/F (Gender)</td><td bgcolor='$bgcolor2'>
<input name='EDIT$gender' value='$gender'>
";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>Fave Band</td><td bgcolor='$bgcolor2'>
<input name='EDIT$faveband' value='$faveband'>
";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>Fave TV Show</td><td bgcolor='$bgcolor2'>
<input name='EDIT$favetvshow' value='$favetvshow'>
";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>Fave Game</td><td bgcolor='$bgcolor2'>
<input name='EDIT$favegame' value='$favegame'>
";

echo "</td></tr><tr><td bgcolor='$bgcolor1'>About Me</td><td bgcolor='$bgcolor2'>
<input name='EDIT$about_me' value='$about_me'>
";

echo "</td></tr></table>";
}//end if
echo "</select></tr></td>";

echo '<input type="submit" name = "submit" value="Update Profile">';
echo '</form>';

if (isset($_POST[submit]))//this will check the form to see if the button was pressed.
{
echo "button pressed";
echo "$_POST[EDIT_fullname]";
/*
I can't remember if sql will complain about thsi fullname = '$_POST[EDIT_fullname]' if it does then try setting $fullname equal to one of these {$_POST[EDIT_fullname]} or {$_POST['EDIT_fullname']}
You have to do this for every field because what's happening is that php is interpreting '$location' as a variable you just declared. So it asigns it a NULL value instead of the value in the form.
But you want the form values. In order to do that you need to access the POST data variables in that form.
To access POST data simply do this: $_POST[form_variable]

change this so that you submit all the POST data $SQL = "UPDATE ALS_signup SET fullname = '$_POST[EDIT_fullname]', location = '$location', gender = '$gender', faveband = '$faveband', favetvshow = '$favetvshow', favegame = '$favegame', about_me = '$about_me' WHERE username = '$username'";
*/
$result = mysql_query($SQL) or die(mysql_error());
echo "<b>profile updated</b>";
}
else
echo "button not pressed";

?>

--Nick

del_boy

11:00 am on May 15, 2005 (gmt 0)

10+ Year Member



Thanks For Your Help Nick, but for some reason the 'input' boxes and the cells next to them with the name of the box are not showing up all i am getting is a 'update button' and the words 'button not pressed', IM NOT SURE WHY! but to help you understand more here is what the html that it creates:

---------------------------------------------
<table width="100%" align="center">

<form name="update" action="update.php" method="post">

<input type="submit" name = "submit" value="Update Profile">

</form>

button not pressed
---------------------------------------------

Thats All im getting, Any Ideas?

del_boy

7:04 pm on May 15, 2005 (gmt 0)

10+ Year Member



Well i have been playing around with the scipt and thanks to you i have got it 80%-90% working the only tihng that dont work is that it is not updating the database, i think it might be somthing to do with the SQL code!

[edited by: ergophobe at 8:41 pm (utc) on May 15, 2005]
[edit reason] long code dump - see forum charter [/edit]

ergophobe

8:51 pm on May 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The basic thing to do is to simplify the script to the point where you can ID which part is causing the problem. Then if you can't figure out how to fix it, post that part of the code.

For some suggestions on how to figure that out, see

[webmasterworld.com...]

ramoneguru

3:33 am on May 16, 2005 (gmt 0)

10+ Year Member



Sorry ergophobe. After I posted I knew that didn't look right. Well del_boy your main problem is that the code is somewhat hard to read (for me at least). Use more simplifed statements. Instead of creating a seperate variable to store the query data, then echo that data simply just echo the query data.

Also, since this profile has a lot of information in it try and develop it incrementally (you probably already did this). Add one textbox with data, then test that, then add another box, and so on.

Add onto this as you need to.

function getUserForm($bgcolor1, $bgcolor2)
{
$getUserProfile = mysql_query("SELECT * FROM ALS_signup WHERE username = '$username'")or die(mysql_error());

$userProfile = mysql_fetch_array($getUserProfile);

echo '<table width="100%" align="center">';
echo '<form name="update" action="update2.php" method="post">';

print "<tr><td WIDTH=120 bgcolor='$bgcolor1'>UserName</td><td bgcolor='$bgcolor2'>$userProfile[fullname]";
print "</td></tr><tr><td>User ID</td><td>$userProfile[id]";

echo '</td></tr><tr><td>Sign Up Date</td><td>';
echo '<input type = "text" name="signupdate" value= $userProfile[signupdate]>';

echo '<input type="submit" name = "submit" value="Update Profile">';
echo '</form>';

}//end getUserForm

--Nick

del_boy

3:16 am on May 19, 2005 (gmt 0)

10+ Year Member



Well Nick/RamoneGuru,

Your Script Helped me treat, thank you so much for your help, i will be looking into using more simplifed statements like you said as i went back to my code and confused myself over what was what.

Once again thank you very much, all my website visitor are very happy to be able to update there profiles at last.

Del