Jump to content


Photo

Edit Information Form


  • Please log in to reply
3 replies to this topic

#1 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 20 March 2006 - 03:55 AM

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.

#2 skatermike21988

skatermike21988
  • Members
  • PipPipPip
  • Advanced Member
  • 100 posts
  • LocationFlorida

Posted 20 March 2006 - 04:59 AM

[!--quoteo(post=356571:date=Mar 19 2006, 10:55 PM:name=JustinK101)--][div class=\'quotetop\']QUOTE(JustinK101 @ Mar 19 2006, 10:55 PM) View Post[/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:

<?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> 

Simply Have A Form Post The Variables To This php page and your good to go

#3 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 20 March 2006 - 05:16 AM

Hey Mike, I dont follow. :) LOL Sorry.

Here is my form action:

<form name="update_account" method="POST" action="<? $_SERVER['QUERY_STRING'] ?>">

Here is my mysql querry to grab all the user information:
$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);

Then all my fields values are set to this:
<input.... value="<? echo $account['field_name']; ?>"..>

#4 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 20 March 2006 - 06:11 AM

I think I got it. :) LOL

Just when the form was submitted, I just updated all the fields:

So:

$account['first_name'] = $first_name;




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users