PRodgers4284 Posted February 7, 2008 Share Posted February 7, 2008 I have a registration from to signup to my website, i want users to be able to edit their account, i have an view account page link when the user logs in. I was wondering would it be hard to use the register form again in the view account page and post the data back and be able to edit it? Phil Quote Link to comment https://forums.phpfreaks.com/topic/89923-solved-editing-a-useraccount-help/ Share on other sites More sharing options...
phpSensei Posted February 7, 2008 Share Posted February 7, 2008 Make a Form for all fields you want them to edit: Password Email Name ...etc Then validate the form and use a UPDATE query to update the users account with the new info. E.G. $query = "UPDATE `user` SET `password` = $password". "WHERE user = '$id'"; Quote Link to comment https://forums.phpfreaks.com/topic/89923-solved-editing-a-useraccount-help/#findComment-460985 Share on other sites More sharing options...
revraz Posted February 7, 2008 Share Posted February 7, 2008 And to get the ID, I use a session instead of a $_GET. Quote Link to comment https://forums.phpfreaks.com/topic/89923-solved-editing-a-useraccount-help/#findComment-460987 Share on other sites More sharing options...
phpSensei Posted February 7, 2008 Share Posted February 7, 2008 Yes when the user logs in, make sure the session 'user_id' is set, this will make it more simple for you. <?php $id = $_SESSION['user_id']; // Example: User id is 2 $password = md5($_POST['password']); $query = "UPDATE `user` SET `password` = $password". "WHERE user = '$id'"; mysql_query($query) or die(mysql_error()); // And your done ?> You can do multiple fields also by doing <?php $id = $_SESSION['user_id']; // Example: User id is 2 $password = md5($_POST['password']); $email = $_POST['email']; $query = "UPDATE `user` SET `password` = $password, `email` = $email WHERE user = '$id'"; mysql_query($query) or die(mysql_error()); // And your done ?> Quote Link to comment https://forums.phpfreaks.com/topic/89923-solved-editing-a-useraccount-help/#findComment-460989 Share on other sites More sharing options...
PRodgers4284 Posted February 7, 2008 Author Share Posted February 7, 2008 I have a registration form done with the validation i need, i just want to post the details entered by the user to be posted back when the user accesses the "myaccount" page. Can i use the registration form again for the account page by posting the data back to it again displaying what the user entered at registration? here is my code for the registration: <?php session_start(); include("database.php"); include("login.php"); //declare at top of page or in included file function usernameTaken($username,&$conn){ if(get_magic_quotes_gpc()){ $username = stripslashes($username); } $username = mysql_real_escape_string($username); $q = "select username from users where username = '$username'"; $result = mysql_query($q,$conn); return (mysql_num_rows($result) > 0); } function emailTaken($email){ global $conn; if(!get_magic_quotes_gpc()){ $email = addslashes($email); } $q = "select username from users where email = '$email'"; $result = mysql_query($q,$conn); return (mysql_numrows($result) > 0); } function mobileTaken($mobile,&$conn){ if(get_magic_quotes_gpc()){ $mobile = stripslashes($mobile); } $mobile = mysql_real_escape_string($mobile); $q = "select mobile from users where mobile = '$mobile'"; $result = mysql_query($q,$conn); return (mysql_num_rows($result) > 0); } ?> <!--Register Form --> <?php $error_stat = 0; $username_message = ''; $password_message = ''; $forename_message = ''; $surname_message = ''; $email_message = ''; $mobile_message = ''; $dob_message = ''; $location_message = ''; $checkbox_message = ''; if (isset($_POST['submit'])) { $username = $_POST['username']; $password1 = $_POST['password']; $password2 = $_POST['password2']; $md5password = md5($_POST['password']); $forename = $_POST['forename']; $surname = $_POST['surname']; $email = $_POST['email']; $mobile = $_POST['mobile']; $dob = $_POST['dob']; $location = $_POST['location']; $ip = $_SERVER['REMOTE_ADDR']; //Error checking //Username check) if (empty($username)) { //Set the error_stat to 1, which means that an error has occurred $error_stat = 1; //Set the message to tell the user to enter a username $username_message = '*Please enter a username*'; } if(usernameTaken($username,$conn)) { $error_stat = 1; $username_message = '*User name is taken, choose another one*'; } $username = $_POST['username']; $username = trim($username); if (strlen($username) > 12){ $error_stat = 1; $username_message = '*The username must be 12 characters or less*'; } $username = $_POST['username']; $username = trim($username); if (strlen($username) < 4){ $error_stat = 1; $username_message = '*Username must be at least 4 characters*'; } else if ( preg_match( '/\W/', $username)){ $error_stat = 1; $username_message = '*Invalid username, letters only, no spaces*'; } //Password check) if($password1 != $password2) { $error_stat = 1; $password_message = '*Passwords don\'t match*'; } if (empty($password1)) { //Set the error_stat to 1, which means that an error has occurred $error_stat = 1; //Set the message to tell the user to enter a username $password_message = '*Please enter a password*'; } if(!$password1 || !$password2) { $error_stat = 1; $password_message = '*Please enter both passwords*'; } $password = $_POST['password']; $password = trim($password); if (strlen($password) < 4){ $error_stat = 1; $password_message = '*Password must be at least 4 characters*'; } else if ( preg_match( '/\W/', $password)){ $error_stat = 1; $password_message = '*Invalid password, letters only, no spaces*'; } //Forename check) if (empty($forename)) { //Set the error_stat to 1, which means that an error has occurred $error_stat = 1; //Set the message to tell the user to enter a username $forename_message = '*Please enter your forename*'; } else if (ctype_digit($forename)) { $error_stat = 1; $forename_message .= '*Invalid forename*'; } else if ( preg_match( '/\W/', $forename)){ $error_stat = 1; $forename_message = '*Invalid forename, letters only, no spaces*'; } $forename = $_POST['forename']; $forename = trim($forename); if (strlen($forename) > 12){ $error_stat = 1; $forename_message = '*The forename must be 12 characters or less*'; } //Surname check) if (empty($surname)) { //Set the error_stat to 1, which means that an error has occurred $error_stat = 1; //Set the message to tell the user to enter a username $surname_message = '*Please enter your surname*'; } else if (ctype_digit($surname)) { $error_stat = 1; $surname_message .= '*Invalid surname*'; } else if ( preg_match( '/\W/', $surname)){ $error_stat = 1; $surname_message = '*Invalid surname, letters only, no spaces*'; } $surname = $_POST['surname']; $surname = trim($surname); if (strlen($surname) > 12){ $error_stat = 1; $surname_message = '*The surname must be 12 characters or less*'; } //Email check) if (empty($email)) { //Set the error_stat to 1, which means that an error has occurred $error_stat = 1; //Set the message to tell the user to enter an email address $email_message = '*Please enter your email address*'; } //Check format of email address entered else if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)){ $error_stat = 1; //Set the message to tell the user to enter a valid email address $email_message = '*Invalid Email Address*'; } if(emailTaken($email,$conn)) { $error_stat = 1; $email_message = '*Email is taken please choose another one*'; } $email = $_POST['email']; $email = trim($email); if (strlen($email) > 30){ $error_stat = 1; $email_message = '*The email address must be 30 characters or less*'; } //Mobile number check) if (empty($mobile)) { //Set the error_stat to 1, which means that an error has occurred $error_stat = 1; //Set the message to tell the user to enter a dob $mobile_message = '*Please enter your mobile number*'; } else if (!ctype_digit($mobile)) { $error_stat = 1; $mobile_message .= '*The mobile phone number must be only numbers*'; } if(mobileTaken($mobile,$conn)) { $error_stat = 1; $mobile_message = '*Mobile already in use, choose another one*'; } $mobile = $_POST['mobile']; $mobile = trim($mobile); if (strlen($mobile) > 11){ $error_stat = 1; $mobile_message = '*Invalid mobile number*'; } $mobile = $_POST['mobile']; $mobile = trim($mobile); if (strlen($mobile) < 11){ $error_stat = 1; $mobile_message = '*Invalid mobile number, must be 11 numbers*'; } //DOB check) if (empty($dob)) { //Set the error_stat to 1, which means that an error has occurred $error_stat = 1; //Set the message to tell the user to enter a dob $dob_message = '*Please enter your date of birth*'; } //Check the format and explode into $parts elseif (!ereg("^([0-9]{2})/([0-9]{2})/([0-9]{4})$", $dob, $parts)){ $error_stat = 1; //Set the message to tell the user the date is invalid $dob_message = '*Invalid dob, must be DD/MM/YYYY format*'; } elseif (!checkdate($parts[2],$parts[1],$parts[3])) { $error_stat = 1; //Set the message to tell the date is invalid for the month entered $dob_message = '*Invalid dob, month must be between 1-12*'; } elseif (intval($parts[3]) < 1948 || intval($parts[3]) > intval(date("Y"))) { $error_stat = 1; //Set the message to tell the user the date is invalid for the year entered $dob_message = '*Invalid dob, year must 1948 onwards*'; } //Terms and condition check) if(!isset($_POST['checkthis'])){ //Set the error_stat to 1, which means that an error has occurred $error_stat = 1; //Set the message to tell the user to enter a dob $checkbox_message = '*You did not accept terms and conditions*'; } if ($location == 'Please Select'){ //Set the error_stat to 1, which means that an error has occurred $error_stat = 1; $location_message = '*Please select a location*'; } //Then, only run the query if there were no errors (if $error_stat still equals 0) if ($error_stat == 0) { mysql_query("INSERT INTO users (username, password, forename, surname, email, mobile, dob, location, ipaddress) VALUES ('$username', '$md5password', '$forename', '$surname', '$email', '$mobile', '$dob', '$location', '$ip')"); echo "<h3>Registration Successful!</h3>"; echo "<p>Thankyou, <b>$username</b>,registration was successful</p>"; echo "<p>login.</p>"; echo "<a href=\"index.php\">Login</a>"; } } //Then, for the form, only show it if 1) the form hasn't been submitted yet OR 2) there is an error if (!isset($_POST['submit']) || $error_stat == 1) { ?> <!--this will show whatever is in the $message variable --> <form method="post" class="registerform" action=""> <fieldset> <label for="username">Username:</label> <input name="username" type="text" id="username" value="<?php echo $_POST['username']; ?>" /> <?php echo "$username_message";?></fieldset> <fieldset> <label for="password">Password:</label> <input name="password" type="password" id="password" value="<?php echo $_POST['password']; ?>"/> <?php echo "$password_message";?></fieldset> <fieldset style="width: 602; height: 29"> <label for="password">Re-type Password:</label> <input name="password2" type="password" id="password2" value="<?php echo $_POST['password2']; ?>" /> </fieldset> <fieldset> <label for="forename">Forename:</label> <input name="forename" type="text" id="forename" value="<?php echo $_POST['forename']; ?>" /> <?php echo "$forename_message";?></fieldset> <fieldset> <label for="surname">Surname:</label> <input name="surname" type="text" id="surname" value="<?php echo $_POST['surname']; ?>" /> <?php echo "$surname_message";?></fieldset> <fieldset> <label for="email">Email:</label> <input name="email" type="text" id="email" maxlength="30" value="<?php echo $_POST['email']; ?>" /> <?php echo "$email_message";?></fieldset> <fieldset> <label for="mobile">Mobile:</label> <input name="mobile" type="text" id="mobile" value="<?php echo $_POST['mobile']; ?>"/> <?php echo "$mobile_message";?></fieldset> <fieldset> <label for="dob">DOB:</label> <input name="dob" type="text" id="dob" value="<?php echo $_POST['dob']; ?>"/> <?php echo "$dob_message";?></fieldset> <fieldset> <label for="location">Location:</label> <p></p> <select name="location"> <option value="Please Select">Please Select</option> <?php $location_opts = array( "Co.Antrim", "Co.Armagh", "Co.Down", "Co.Fermanagh", "Co.Londonderry", "Co.Tyrone", ); foreach($location_opts as $opt){ $selected = $_POST['location'] == $opt ? " selected=true":""; print "<option value=\"{$opt}\"{$selected}>{$opt}</option>"; } ?> </select> <?php echo "$location_message";?><?php echo $error['location']; ?></</fieldset> <p></p> <fieldset> <label for="term">Terms and Conditions:</label> <p></p> <textarea readonly>Terms and Conditions go here</textarea> <p></p> <p class="terms"><input type="checkbox" name="checkthis" value="<?php echo $_POST['checkthis']; ?>">Accept</label></p> <?php echo "$checkbox_message";?><fieldset> <p></p> <p class="submit"><input type="submit" name="submit" value="Register" /> </fieldset> </form> <?php } ?> Phil Quote Link to comment https://forums.phpfreaks.com/topic/89923-solved-editing-a-useraccount-help/#findComment-460995 Share on other sites More sharing options...
Wolphie Posted February 7, 2008 Share Posted February 7, 2008 It wouldn't be too hard to re-use the register form. But instead of using the INSERT statement, you'd use UPDATE Quote Link to comment https://forums.phpfreaks.com/topic/89923-solved-editing-a-useraccount-help/#findComment-460997 Share on other sites More sharing options...
PRodgers4284 Posted February 7, 2008 Author Share Posted February 7, 2008 It wouldn't be too hard to re-use the register form. But instead of using the INSERT statement, you'd use UPDATE Its the posting of the data that has already been entered at registration that im stuck on, it i can find i way of posting the data back to the form. Quote Link to comment https://forums.phpfreaks.com/topic/89923-solved-editing-a-useraccount-help/#findComment-461014 Share on other sites More sharing options...
phpSensei Posted February 7, 2008 Share Posted February 7, 2008 It wouldn't be too hard to re-use the register form. But instead of using the INSERT statement, you'd use UPDATE Its the posting of the data that has already been entered at registration that im stuck on, it i can find i way of posting the data back to the form. <?php $id = $_SESSION['id']; $query = mysql_query("SELECT * FROM users WHERE id = '$id'"); $row = mysql_fetch_array($query); echo 'Username: <input type="text" name="username" value="'.$row['username'].'"/>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/89923-solved-editing-a-useraccount-help/#findComment-461025 Share on other sites More sharing options...
PRodgers4284 Posted February 7, 2008 Author Share Posted February 7, 2008 It wouldn't be too hard to re-use the register form. But instead of using the INSERT statement, you'd use UPDATE Its the posting of the data that has already been entered at registration that im stuck on, it i can find i way of posting the data back to the form. <?php $id = $_SESSION['id']; $query = mysql_query("SELECT * FROM users WHERE id = '$id'"); $row = mysql_fetch_array($query); echo 'Username: <input type="text" name="username" value="'.$row['username'].'"/>'; ?> Thanks for that, really appreciate ur help Quote Link to comment https://forums.phpfreaks.com/topic/89923-solved-editing-a-useraccount-help/#findComment-461033 Share on other sites More sharing options...
phpSensei Posted February 7, 2008 Share Posted February 7, 2008 np. Just make sure you mark the topic solved Quote Link to comment https://forums.phpfreaks.com/topic/89923-solved-editing-a-useraccount-help/#findComment-461035 Share on other sites More sharing options...
PRodgers4284 Posted February 7, 2008 Author Share Posted February 7, 2008 can you u give me some help, not sure where to start off adding the code in, just a few pointers of where to start and i can work the rest, sorry for pestering you btw phil Quote Link to comment https://forums.phpfreaks.com/topic/89923-solved-editing-a-useraccount-help/#findComment-461062 Share on other sites More sharing options...
phpSensei Posted February 7, 2008 Share Posted February 7, 2008 Put this somewhere at the top of your page <?php $id = $_SESSION['id']; $query = mysql_query("SELECT * FROM users WHERE id = '$id'"); $info = mysql_fetch_array($query); Then Lets Say you Want to Show the Username, Just find the inputs lets say Username: <input name="username" type="text" id="username" value="<?php echo $info['username']; ?>" /> But dont allow them to change usernames lol, that would cause chaos on your site. Quote Link to comment https://forums.phpfreaks.com/topic/89923-solved-editing-a-useraccount-help/#findComment-461067 Share on other sites More sharing options...
PRodgers4284 Posted February 7, 2008 Author Share Posted February 7, 2008 Thankyou so much Quote Link to comment https://forums.phpfreaks.com/topic/89923-solved-editing-a-useraccount-help/#findComment-461074 Share on other sites More sharing options...
phpSensei Posted February 7, 2008 Share Posted February 7, 2008 Thankyou so much No Problem, Just Click "Topic Solved" On the top of this topic right on the tabs. Quote Link to comment https://forums.phpfreaks.com/topic/89923-solved-editing-a-useraccount-help/#findComment-461080 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.