The code below should get you started. I have deleted some of the text or translated it as the text displayed to the user was in French.
Everything lives inside a html <div>. Mine has the id of "content" in this case.
I use a MySQL database and MYSQL is defined as the connection in a required file (see beginning of the code) You could also replace that line with the connection information and mysql_connect().
You'll have to change the database queries to suit your needs, but what is here should point you in the right direction. Try to get it working. You'll learn a lot!
I'm no expert, so if anyone has any criticisms about my code, please tell me. I still have A LOT to learn and would love some feedback.
Good luck!
Code:
<div id="content">
<?php
require_once(MYSQL); // db connection
if ( isset($_POST['update_u']) ) {
//Required Fields
$errors = array();
if ( preg_match('/^[^0-9]{2,30}$/i', $_POST['first_name']) ) {
$first_name = mysql_prep(ucwords(trim(substr($_POST['first_name'],0,30))));
} else {
$errors[] = '<p class="error"></p>';
}
if ( preg_match('/^[^0-9]{2,30}$/i', $_POST['last_name']) ) {
$last_name = mysql_prep(ucwords(trim(substr($_POST['last_name'],0,30))));
} else {
$errors[] = '<p class="error"></p>';
}
if ( empty($errors) ) {
$q = "UPDATE `user_info`
SET `first_name` = '$first_name',
`last_name` = '$last_name'
WHERE `user_info`.`u_id` = {$_SESSION['U_ID']} LIMIT 1 ";
$r = mysql_query($q);
if ($r) {
echo "<p></p>";
} else {
echo "<p class=\"error\"></p>";
}
} else {
// Errors
foreach ($errors as $msg) {
echo '<p class="error">' . "$msg" . '</p>';
}
}
} elseif ( isset($_POST['update_p']) ) { // -------------------- password update branch -----------------------
//Required Fields
$errors = array();
if ( preg_match('/^\w{6,20}$/', $_POST['new_pass']) ) {
if ( $_POST['new_pass'] != $_POST['repeat_new_pass'] ) {
$errors[] = "";
} else {
$new_pass = md5(trim($_POST['new_pass']));
}
} else {
$errors[] = '';
}
if ( empty($errors) ) { // No problems with password
// Change password in db
$q = "UPDATE `users`
SET `pass_word` = '$new_pass'
WHERE `users`.`u_id` = {$_SESSION['U_ID']} LIMIT 1 ";
$r = mysql_query($q);
if ($r) {
echo "<p></p>";
} else {
echo "<p></p>";
}
} else { // Display Errors
foreach ($errors as $msg) {
echo '<p class="error">' . "$msg" . '</p>';
}
}
} elseif ( isset($_POST['delete_u']) ) { // -------------------- Delete User branch -----------------------
if ( isset($_POST['unsubscribe']) && $_POST['unsubscribe'] == "1") { // Verify check box was checked
$q = "DELETE FROM `users`, `user_info`
USING `users`, `user_info`
WHERE `users`.`u_id` = `user_info`.`u_id`
AND `users`.`u_id` = {$_SESSION['U_ID']} ";
$r = mysql_query($q);
if ( $r ) {
echo '<p></p>';
$_SESSION = array();
session_destroy();
echo '</div> <!-- end content -->';
include_once($_SERVER['DOCUMENT_ROOT'] . '/includes/sidebar.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/includes/footer.php');
exit();
} else { // Problem
echo '<p></p>';
}
} else {
echo '<p class="error">You must check verify your request to unsubscribe by marking the checkbox titled unsubscribe.</p>';
}
}
?>
<?php
$q = "SELECT *
FROM `user_info`
WHERE `u_id` = {$_SESSION['U_ID']} LIMIT 1 ";
$r = mysql_query($q);
if ( mysql_num_rows($r) == 1 ) {
$row = mysql_fetch_assoc($r);
?>
<h2><?php echo $row['first_name'] . " " . $row['last_name']; ?></h2>
<p>Your email address : <?php echo $row['email']; ?></p>
<form id="update" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<legend></legend>
<label for="first_name">First Name</label>
<input type="text" name="first_name" id="first_name" value="<?php echo $row['first_name']; ?>" />
<label for="last_name">Last Name</label>
<input type="text" name="last_name" id="last_name" value="<?php echo $row['last_name']; ?>" />
<input type="hidden" name="update_u" value="1" />
<p><input class="submit" name="submit" type="submit" value="Submit" /></p>
</fieldset>
</form> <!-- end update -->
<form id="change_pass" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<legend></legend>
<label for="new_pass">New Pass Word. Must be between 6 and 20 characters and contain letters and numbers only.</label>
<input type="password" name="new_pass" id="new_pass" />
<label for="repeat_new_pass">Repeat New Password</label>
<input type="password" name="repeat_new_pass" id="repeat_new_pass" />
<input type="hidden" name="update_p" value="1" />
<p><input class="submit" type="submit" name="submit" value="Submit" /></p>
</fieldset>
</form> <!-- end change_pass -->
<form id="unsubscribe" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<legend></legend>
<label for="unsubscribe">Click here to unsubscribe<input type="checkbox" class="checkbox" name="unsubscribe" id="unsubscribecb" value="1" /></label>
<input type="hidden" name="delete_u" value="1" />
<p><input class="submit" type="submit" name="submit" value="Valider" /></p>
</fieldset>
</form> <!-- end unsubscribe -->
<p><a href="/"></a></p>
<?php
} else {
// No u_id exists
echo "<p>Error</p>";
echo "</p><a href=\"/\">Cliquez ici pour retourner à l'accueil</a></p>";
} // end if rows query
?>
</div> <!-- end content -->
Bookmarks