Jump to content


Photo

checking if email address exists in mysql [RESOLVED]


  • Please log in to reply
7 replies to this topic

#1 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 12 August 2006 - 07:23 PM

I have a form for my members where they can update their details such as their name and email.

The problem I'm having is checking if the email address has been changed before submitting the form and if it has been changed to make sure the email address doesn't already exist in the database.

I want it to be able to update just the name and keep the email address or just be able to update the email address

This is how far I have got but have got the compare email address wrong
<?
session_start();
session_register("session");

if(!isset($session['userid'])){
echo "<center><font face='Verdana' size='2' color=red>Sorry, Please login and use this page </font></center>";
exit;
}

// This is displayed if all the fields are not filled in
$empty_fields_message = "<p>Please go back and complete all the fields in the form.</p>Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back";

// Convert to simple variables 
$first_name = stripslashes($_POST['first_name']); 
$last_name = stripslashes($_POST['last_name']);
$email_address = stripslashes($_POST['email_address']);

if (!isset($_POST['first_name'])) {
?>
<h2>Update personal details!</h2>

<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">

	<p class="style3"><label for="first_name" style="width:8em">First Name:</label>
        <input type="text" title="Please enter your first name" name="first_name" size="30" value="<? echo $_SESSION['firstname']; ?>"/></p>

        <p class="style3"><label for=last_name" style="width:8em">Second Name:</label>
        <input type="text" title="Please enter your last name" name="last_name" size="30" value="<? echo $_SESSION['lastname']; ?>"/></p>

        <p class="style3"><label for="email_address" style="width:8em">Email address:</label>
        <input type="text" title="Enter your email address" name="email_address" size="30" value="<? echo $_SESSION['emailaddress']; ?>"/></p>		

	<p class="style3"><label title="Login">
    <input type="submit" value="Update" style="margin-left:97px" class="submit-button"/></label></p>
</form>
<?php
}
elseif (empty($first_name) || empty($last_name) || empty($email_address))  {

    echo $empty_fields_message;

}

else {

include 'includes/connection.php';

if (strcmp( $email_address,$_SESSION['emailaddress'] ) !=0){
    if(mysql_num_rows(mysql_query("SELECT email_address FROM users WHERE email_address = '$email_address'"))){
    $msg=$msg."Your email address has already been used by another member in our database. Please submit a different Email address!!<BR>";
    $status= "NOTOK";}
    }
if($status<>"OK"){ 
   echo "<font face='Verdana' size='2' color=red>$msg</font><br><input type='button' class='submit-button' value='Retry' onClick='javascript:history.go(-1)'>";
   exit();
   }

Has anybody got any ideas how i could ignore the email address if the it matches the email in the database and if it is changed check for an existing email
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#2 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 12 August 2006 - 10:48 PM

I got this code to check if both the session email and the email text box match and returns the text box email but how do i check to see if the any of the name fileds have changed even if the email hasn't/
if (strcmp( $email_address,$_SESSION['emailaddress'] ) !=0){

	echo $email_address;
    }
else
    {
	echo "No changes made";
    }
}

If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#3 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 12 August 2006 - 10:54 PM

checking something in the database
<?php
$query = "SELECT * FROM table WHERE email = '".$email."'";

$result = mysql_query($query);

if(mysql_num_rows($result) == 0){
echo "That is not in the database";
}else{
echo "The email address is in the database";
}

Tell me the problem, I will try tell you the solution

#4 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 13 August 2006 - 12:13 AM

Just another quick question......
How do I select a field from the database and put it into a variable such as $id

I want to get the id from a record and put it into a variable

$query = "SELECT * FROM users WHERE id = '".$_SESSION['userid']."'";

$result = mysql_query($query);

If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#5 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 13 August 2006 - 01:40 AM

$query = "SELECT * FROM users WHERE id = '".$_SESSION['userid']."'";

$result = mysql_query($query);
$id = mysql_fetch_assoc($result);
$id = $id['id'];

Why doesn't anyone ever say hi, hey, or whad up world?

#6 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 13 August 2006 - 09:04 AM

$query = "SELECT * FROM users WHERE id = '".$_SESSION['userid']."'";

$result = mysql_query($query);
$id = mysql_fetch_assoc($result);
$id = $id['id'];


no Exact
$query = "SELECT * FROM users WHERE id = '".$_SESSION['userid']."'";

$result = mysql_query($query);
while(row = mysql_fetch_assoc($result)){
$id = $row['id'];
}

Tell me the problem, I will try tell you the solution

#7 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 13 August 2006 - 11:01 AM

I get this error message now

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/adrock/public_html/jack/edit_profile.php on line 49
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#8 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 13 August 2006 - 01:27 PM

do
echo mysql_error();

Tell me the problem, I will try tell you the solution




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users