Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hello,

 

The text in this chapter seems to indicate that a user can just change their name or whatever and they don't have to touch the email address.

 

I would like my form set up so that the user can do one or the other.

 

However, when testing my page the user is not allowed to only submit a change of username. I get - This email has already been registered.

 

I have fooled around with the code and now back to my original which is follows -

 

<?php

 

// Check if the form has been submitted:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

 

$errors = array();

 

// Check for a username:

if (empty($_POST['username'])) {

$errors[] = 'You forgot to enter your username.';

} else {

$un = mysqli_real_escape_string($connect, trim($_POST['username']));

}

 

// Check for an email address:

if (empty($_POST['email'])) {

$errors[] = 'You forgot to enter your email address.';

} else {

$e = mysqli_real_escape_string($connect, trim($_POST['email']));

}

 

if (empty($errors)) { // If everything's OK.

 

// Test for unique email address:

$q = "SELECT id FROM users WHERE email='$e' AND id={$_SESSION['user_id']}";

$r = @mysqli_query($connect, $q);

if (mysqli_num_rows($r) == 0) {

 

// Make the query:

$q = "UPDATE users SET username='$un', email='$e' WHERE id={$_SESSION['user_id']} LIMIT 1";

$r = @mysqli_query ($connect, $q);

if (mysqli_affected_rows($connect) == 1) { // If it ran OK.

 

// Print a message:

echo '<p>The user has been edited.</p>';

 

} else { // If it did not run OK.

echo '<p class="error">The user could not be edited due to a system error. We apologize for any inconvenience.</p>'; // Public message.

echo '<p>' . mysqli_error($connect) . '<br />Query: ' . $q . '</p>'; // Debugging message.

}

 

} else { // Already registered.

echo '<p class="error">The email address has already been registered.</p>';

}

 

} else { // Report the errors.

 

echo '<p class="error">The following error(s) occurred:<br />';

foreach ($errors as $msg) { // Print each error.

echo " - $msg<br />\n";

}

echo '</p><p>Please try again.</p>';

 

} // End of if (empty($errors)) IF.

 

} // End of submit conditional.

 

// Always show the form...

 

//id={$_SESSION['user_id']}

 

// Retrieve the user's information:

$q = "SELECT username, email FROM users WHERE id={$_SESSION['user_id']}";

$r = @mysqli_query ($connect, $q);

 

if (mysqli_num_rows($r) == 1) { // Valid user ID, show the form.

 

// Get the user's information:

$row = mysqli_fetch_array ($r, MYSQLI_NUM);

 

// Create the form:

echo '<form action="MemberProfileTBR.php" method="post">

 

<p>Userame: <br />

<input type="text" name="username" size="40" maxlength="40" value="' . $row[0] . '" /></p>

 

<p>Email Address: <br />

<input type="text" name="email" size="40" maxlength="60" value="' . $row[1] . '" /> </p>

 

<p><input type="submit" name="submit" value="Edit" /></p>

 

<input type="hidden" name="id" value="' . $_SESSION['user_id'] . '" />

 

</form>';

 

} else { // Not a valid user ID.

echo '<p class="error">This page has been accessed in error.</p>';

}

 

mysqli_close($connect);

 

?>

 

Thanks,

 

Marie

Link to post
Share on other sites

Well half was through the php script you have

 

// Test for unique email address:

$q = "SELECT id FROM users WHERE email='$e' AND id={$_SESSION['user_id']}";

$r = @mysqli_query($connect, $q);

if (mysqli_num_rows($r) == 0) {

 

therefore if the email address is found in the users TABLE the below script will not run, as mysql has to return zero rows to continue into while loop.

Link to post
Share on other sites

Thank you for your quick reply.

 

if (mysqli_num_rows($r) == 0) {

 

I changed the "0" to a "1" in the above line and then the user was allowed to just change their username. However IF then the user decides to then change their email address it returns the message that the email has already been registered even though there is definitely no email at all like the one they are entering in the data base that I have set up.

 

So I will keep working on this.

 

Marie

Link to post
Share on other sites

Yes that is true the email will show already registered, so you need to do a check on whether it is the actual user changing this email and if so that allow change. The above code really needs to be completely rewritten.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...