Current location: Hot Scripts Forums » Programming Languages » PHP » Validate email on Mysql and Redirect


Validate email on Mysql and Redirect

Reply
  #1 (permalink)  
Old 01-17-04, 06:57 AM
dihan dihan is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 267
Thanks: 0
Thanked 0 Times in 0 Posts
Validate email on Mysql and Redirect

I'm using the following script in order to insert: name, email, mobile number. How can I check to see if the email already exisit and if so redirect to another page?

PHP Code:

<?php

if (!isset($_POST['submit'])) { 
echo
'<BODY bgColor=#155271 leftMargin=0 topMargin=0 rightMargin=0 bottomMargin=0 marginwidth="0" marginheight="0">
<form name="subscribe" method="POST" action="'
.$_SERVER['PHP_SELF'].'">
  <input type="hidden" name="list" value="select">
  <input type="hidden" name="list_type" value="list">
  <input type="hidden" name="list_status" value="1">
  <TABLE width=289 height="93" border=0 cellPadding=0 cellSpacing=0>
    <TBODY>
      <TR> 
        <TD width="4" rowspan="5"></TD>
      </TR>
      <TR> 
        <TD>
<TABLE width="100%" height="0" border=0 cellPadding=3 cellSpacing=1>
            <TBODY>
              <TR> 
                <TD width="70" height="23" class=mainTextBig>Name :</TD>
                <TD width="200" class=frmInput><input name="name" type="text" class=mainFormBox id="name" 
                        style="WIDTH: 200px"></TD>
              </TR>
              <TR> 
                <TD width="70" height="23" class=mainTextBig>Email:</TD>
                <TD class=frmInput><input name="email" type="text" class=mainFormBox id="email" 
                        style="WIDTH: 200px"></TD>
              </TR>
              <TR> 
                <TD width="70" height="23" class=mainTextBig>Mobile No:</TD>
                <TD class=frmInput><input name="mobile" type="text" class=mainFormBox id="mobile" 
                        style="WIDTH: 200px"></TD>
              </TR>
            </TBODY>
          </TABLE></TD>
      </TR>
      <TR> 
        <TD><div align="right"> 
        <input type="hidden" name="submit" value="done">
            <input name="submit" type=image value="Submit" src="submit.jpg" alt="Subscribe" target="_self" align=Submit width=109 height=20>
          </div></TD>
      </TR>
    </TBODY>
  </TABLE>
        </form>'
;
        
?>
        
<script langauage="Javascript" type="text/javascript">
var frmvalidator = new Validator("subscribe");
frmvalidator.addValidation("name","req","Please enter your Name");
frmvalidator.addValidation("email","req","Please enter your email address"); 
frmvalidator.addValidation("mobile","req","Please enter your mobile number"); 
frmvalidator.addValidation("email","email");
</script>

<?php

} else {
    
$name=trim(addslashes($_POST['name']));
$email=trim(addslashes($_POST['email']));    
$mobile=trim(addslashes($_POST['mobile']));

$list=trim(addslashes($_POST['list']));
$list_type=trim(addslashes($_POST['list_type']));
$list_status=trim(addslashes($_POST['list_status']));

$link mysql_connect('localhost','user''password'
or die(
"Could not connect"); 
mysql_select_db('select_db'
or die(
"Could not select database"); 

$insert=mysql_query("INSERT INTO dada_subscribers (name, email, mobile, list, list_type, list_status) VALUES ('$name', '$email', '$mobile', '$list', '$list_type', '$list_status')")or 
die(
'couldn\'t Insert record into Subscribers database :'.mysql_error());

    echo
'The Record was inserted succesfully into Database'
    } 
?>
Reply With Quote
  #2 (permalink)  
Old 01-17-04, 07:35 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
this is very easy ..
you can either make the field email as a 'unique' index in your table , so mysql will generate an error if the same e-mail was inserted , or use a small SELECT query to check after the form has been submited ..
like this :
PHP Code:

//this one after the ELSE (meaning after form submittion)

} else { 
$name=trim(addslashes($_POST['name']));
$email=trim(addslashes($_POST['email']));    
$mobile=trim(addslashes($_POST['mobile']));

$list=trim(addslashes($_POST['list']));
$list_type=trim(addslashes($_POST['list_type']));
$list_status=trim(addslashes($_POST['list_status']));

$link mysql_connect('localhost','user''password') or die("Could not connect");
mysql_select_db('select_db') or die("Could not select database");
 
$check=mysql_query("SELECT email FROM dada_subscribers WHERE emial='$email'")or die(mysql_error());

$num=mysql_num_rows($check);

 if ( 
$num == ) {
   
//this should redirect , because we found the same e-mail in DB ..
  
header("Location: page.php");
 } else {
//otherwise we insert the new record
 
$insert=mysql_query("INSERT INTO dada_subscribers (name, email, mobile, list, list_type, list_status) VALUES ('$name', '$email', '$mobile', '$list', '$list_type', '$list_status')")or
die(
'couldn\'t Insert record into Subscribers database :'.mysql_error());

    echo
'The Record was inserted succesfully into Database';
 }

__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]

Last edited by NeverMind; 01-17-04 at 07:37 AM.
Reply With Quote
  #3 (permalink)  
Old 01-17-04, 09:22 AM
dihan dihan is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 267
Thanks: 0
Thanked 0 Times in 0 Posts
Slight error on the redirect:

Warning: Cannot add header information - headers already sent by (output started at /home/httpd/vhosts/selectguestlist.com/httpdocs/index/subscribe.php:8) in /home/httpd/vhosts/selectguestlist.com/httpdocs/index/subscribe.php on line 86


PHP Code:

$link mysql_connect('localhost','select_user''password') or die("Could not connect"); 

mysql_select_db('select_db') or die("Could not select database"); 

$check=mysql_query("SELECT * FROM `dada_subscribers` WHERE 1 AND `email` LIKE '$email' LIMIT 0 , 30")or die(mysql_error()); 

$num=mysql_num_rows($check); 

if ( 
$num == ) { 
   
//this should redirect , because we found the same e-mail in DB .. 
  
header("Location: http://www.selectguestlist.com/index/subscribeFailed.htm"); 
} else { 
//otherwise we insert the new record 
$insert=mysql_query("INSERT INTO dada_subscribers (name, email, mobile, list, list_type, list_status) VALUES ('$name', '$email', '$mobile', '$list', '$list_type', '$list_status')")or 
die(
'couldn\'t Insert record into Subscribers database :'.mysql_error()); 

     echo 
'<meta http-equiv=\'refresh\' content=\'1; url=http://www.selectguestlist.com/index/subscribeSuccessful.htm\'>'



?> 
Reply With Quote
  #4 (permalink)  
Old 01-18-04, 04:29 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
did you output anything?
this message always pops when you output anything before you sent the header() !
even if it was out of the php starting tag '<?php' .. like plain html ..
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #5 (permalink)  
Old 01-18-04, 06:35 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
sorry to double post but I couldn't edit my last post!
anyway, what I wanted to say is you can bypass this problem by printing a notice to the user that this email is already in database , and print a link to the page you wanted :
PHP Code:

//replace this IF with the old redirect one ..

if ( $num == ) {
   
//this should print a notice , because we found the same e-mail in DB ..
echo'The email address you provided is already in use!<BR />';
echo
'please try again with another email address (<a href="page.php">Click Here</a>)';

__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Mail with php with Mysql dihan PHP 10 01-21-04 05:57 PM


All times are GMT -5. The time now is 08:32 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.