Jump to content

[SOLVED] Editing a useraccount help


PRodgers4284

Recommended Posts

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

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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'].'"/>';


?>


Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.