Page 1 of 1

Change E-Mail Adress Code - Error

Posted: Fri Jul 21, 2006 2:26 pm
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'; 
} 
?>

Posted: Fri Jul 21, 2006 6:55 pm
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 

Posted: Sat Jul 22, 2006 12:23 pm
by 4Boredom
sorry im kinda new, what part of my code would that be put into?

Posted: Sat Jul 22, 2006 12:24 pm
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.

Posted: Sat Jul 22, 2006 2:42 pm
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

Posted: Sat Jul 22, 2006 3:28 pm
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...