Change E-Mail Adress Code - Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Change E-Mail Adress Code - Error

Post by 4Boredom »

I am trying to make it so a user can update their current email address. I get this error:

There has been an error creating your account. Please contact the webmaster.Duplicate entry '' for key 2

Here are my 2 codes

editemail.php

Code: Select all

<?php 
session_start(); 
include("userinfo.php");
require("header.php"); 
include("db.php");
?>

<center>
<b>Edit Your E-Mail</b><br><br>

<form name=signupform method=post action=update-email.php> 
  <table width=100% border=0 cellpadding=4 cellspacing=0 align="center"> 
 
    <tr>  
      <td align=left valign=top>Email Address</td> 
      <td><input name=email_address type=text id=email_address></td> 
    </tr>  
	
      <td align=left valign=top> </td> 
      <td><input type=submit name=Submit value=Update!></td> 
    </tr> 
  </table> 
</form>
update-email.php

Code: Select all

<? 

include 'db.php'; 
# grab the POST variables from the HTML form
$email_address = $_POST['email_address']; 

# Any escaped characters?
$email_address = stripslashes($email_address); 

# Any errors in the posted fields? 
if((!$email_address)){
    if(!$email_address){ 
        echo "Email Address is a required field. Please enter it below.<br />"; 
    } 
	include 'update-email.php'; 
    exit(); 
} 

# does this user already exist in the database?  
 $sql_email_check = mysql_query("SELECT email_address FROM users WHERE email_address='$email_address'");
 $email_check = mysql_num_rows($sql_email_check);
  
 if(($email_check > 0) || ($username_check > 0)){ 
     echo "Please fix the following errors: <br />"; 
     if($email_check > 0){ 
         echo "<strong>Your email address has already been used by another member in our database. Please use a different email address!<br />"; 
         unset($email_address); 
     } 
     include 'update-email.php'; // Shows the form again!
     exit(); 
 } 

 # Enter info into the Database. 

$info2 = htmlspecialchars($info); 


$sql = mysql_query("INSERT INTO `users` 
		(`email_address`) VALUES 
		(\"" . $email_address . "\")"); 

		if(!$sql){ 
    echo 'There has been an error creating your account. Please contact the webmaster.'; 
    echo mysql_error();
} else { 
    echo 'E-Mail updated! This is your new login, so remember it! $email_address'; 
} 
?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

One of the fields in the table is set as a primary key but isn't set to auto increment.

Code: Select all

ALTER TABLE `test` CHANGE `yourFieldName` `yourFieldName` INT( 10 ) AUTO_INCREMENT 
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

sorry im kinda new, what part of my code would that be put into?
User avatar
kbrown3074
Forum Contributor
Posts: 119
Joined: Thu Jul 20, 2006 1:36 pm

Post by kbrown3074 »

You need to change your table using the statement he put in the window. It wont go in the code itself..you need to enter it in mysql or modify your table using the admin interface.
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

awesome.... the autoincrement helps... but now...

instead of updating the email of the logged in member.. id = 1... it just posts the email as a new slot in the database under id = 2?

so is that what autoincrement does? I just wanna let the user change their profiles so I was starting with emails then ima gonna adapt a working code to everything else
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Just change

Code: Select all

sql = mysql_query("INSERT INTO `users` 
                (`email_address`) VALUES 
                (\"" . $email_address . "\")");
to

Code: Select all

sql = mysql_query("UPDATE `users` SET 
                email_address = '{$email_address}' WHERE email_address = '{$emailaddress}' ");
You don't really need the id but its still cool...
Post Reply