Search the Community
Showing results for tags 'php coding'.
-
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