JustinK101 Posted March 20, 2006 Share Posted March 20, 2006 Hey guys,So I have a form which stores name, email, address, city, state, etc. The user fills in the form no problem. I use POST as the method.Now I want to have the ability for the user to edit their information. So the issues is that all the fields values are equal to: <? echo $_POST['field_name'] ?> but I need the fields to be populated with the values stored in the mysql database. So I change the value to <? echo $row['field_value'] ?> which works, with one problem. If they screw up a field or don't enter a required field the page reloads and displays an error message, but all the fields get populated with the values from the database instead of the values which they just updated, i.e. the $_POST['field_name']. See because the values are set to $row['field_name']. Basically I need to have all the values first populate from the database and then after that, they need to populate from $_POST['field_name']. I think. I may be way off here. Thanks for the help. Quote Link to comment Share on other sites More sharing options...
skatermike21988 Posted March 20, 2006 Share Posted March 20, 2006 [!--quoteo(post=356571:date=Mar 19 2006, 10:55 PM:name=JustinK101)--][div class=\'quotetop\']QUOTE(JustinK101 @ Mar 19 2006, 10:55 PM) [snapback]356571[/snapback][/div][div class=\'quotemain\'][!--quotec--]Hey guys,So I have a form which stores name, email, address, city, state, etc. The user fills in the form no problem. I use POST as the method.Now I want to have the ability for the user to edit their information. So the issues is that all the fields values are equal to: <? echo $_POST['field_name'] ?> but I need the fields to be populated with the values stored in the mysql database. So I change the value to <? echo $row['field_value'] ?> which works, with one problem. If they screw up a field or don't enter a required field the page reloads and displays an error message, but all the fields get populated with the values from the database instead of the values which they just updated, i.e. the $_POST['field_name']. See because the values are set to $row['field_name']. Basically I need to have all the values first populate from the database and then after that, they need to populate from $_POST['field_name']. I think. I may be way off here. Thanks for the help.[/quote]Try This It Worked For Me:[code]<?php INCLUDE('header.php'); if ($_POST['firstname'] != "") { $firstname = htmlspecialchars($_POST['firstname']); mysql_query("UPDATE users SET firstname='$firstname' WHERE username='$username'") or die (mysql_error()); $_SESSION['firstname'] = $firstname; $cname = "<li>Your First name</li>"; } if ($_POST['lastname'] != "") { $lastname = htmlspecialchars($_POST['lastname']); mysql_query("UPDATE users SET lastname='$lastname' WHERE username='$username'") or die (mysql_error()); $_SESSION['lastname'] = $lastname; $cname1 = "<li>Your Last name</li>"; } if ($_POST['about'] != "") { $hist = nl2br(htmlspecialchars($_POST['hist'])); mysql_query("UPDATE users SET about='$about' WHERE username='$username'") or die (mysql_error()); $_SESSION['hist'] = $hist; $chist = "<li>About You</li>"; } if ($_POST['webs'] != "http://") { $webs = htmlspecialchars($_POST['webs']); mysql_query("UPDATE users SET webs='$webs' WHERE username='$username'") or die (mysql_error()); $_SESSION['webs'] = $webs; $cwebs = "<li>website URL</li>"; } ?> <html><head><title>Change Profile Results</title></head><body> <h1>Change Profile Results:</h1> <?php if (($cname) || ($cstyle) || ($chist) || ($cinfl) || ($copen) || ($cwebs)) { echo "The following items have been updated in your profile:<br /><ul>"; if ($cname) { echo $cname; } if ($cname1) { echo $cname1; } if ($cstyle) { echo $cstyle; } if ($chist) { echo $chist; } if ($cinfl) { echo $cinfl; } if ($copen) { echo $copen; } if ($cwebs) { echo $cwebs; } echo "</ul><br />To view your updated profile, <a href=\"images.php\">click here</a>."; } else { echo "Nothing in your profile has been changed. <a href=\"images.php\">Click here</a> to return to your profile."; } ?> </body></html> [/code]Simply Have A Form Post The Variables To This php page and your good to go Quote Link to comment Share on other sites More sharing options...
JustinK101 Posted March 20, 2006 Author Share Posted March 20, 2006 Hey Mike, I dont follow. :) LOL Sorry.Here is my form action:[code]<form name="update_account" method="POST" action="<? $_SERVER['QUERY_STRING'] ?>">[/code]Here is my mysql querry to grab all the user information:[code]$sql = "SELECT title, first_name, last_name, company_name, mailing_address, city, state, zip, website, email_address, phone_number, fax_number, industry, username, password, UNIX_TIMESTAMP(date_created) as cdate, UNIX_TIMESTAMP(date_last_modified) as mdate, ip_who_created, ip_who_last_modified FROM accounts WHERE id = " . $_SESSION['session_id'] . "";$result = mysql_query($sql);$account = mysql_fetch_assoc($result);[/code]Then all my fields values are set to this:<input.... value="<? echo $account['field_name']; ?>"..> Quote Link to comment Share on other sites More sharing options...
JustinK101 Posted March 20, 2006 Author Share Posted March 20, 2006 I think I got it. :) LOLJust when the form was submitted, I just updated all the fields:So: $account['first_name'] = $first_name; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.