Need help in simple form validation...

Discussion in 'PHP' started by jehzlau, Jan 17, 2008.

  1. #1
    Hi, I need a little help from PHP gurus out there in my code:

    <?php
    
    $errors=0;
    $error="<b>The following errors occured while processing your form input.</b><br /><br />";
    
    $Name = $_POST['Name'];
    $Email = $_POST['Email'];
    $CellNumberPre = $_POST['CellNumberPre'];
    $CellNumber = $_POST['CellNumber'];
    $AlertOn = $_POST['AlertOn'];
    
    if($Name=="" || $Email=="" || $CellNumberPre=="" || $CellNumber=="" || $AlertOn=="" ){
    $errors=1;
    $error.="<p>You did not enter one or more of the required fields. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>";
    }
    
    if(!preg_match("/^\d+$/",$CellNumber)){
    $errors=1;
    $error.="<p>Only numbers are accepted in the cell number field. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>";
    }
    
    if (strlen($CellNumber) < 7){
    $errors=1;
    $error.="<p>Your cellphone number is not valid. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>";
    }
    
    if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$Email)){
    $error.="<p>Invalid email address entered. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>";
    $errors=1;
    }
    
    if($errors==1) echo $error;
    else{
    
    $link = mysql_connect("localhost","jehzlau","password111");
    mysql_select_db("database",$link);
    $query="insert into sms_subscribers (name,email,cnp,cn,alerton) values ('".$Name."','".$Email."','".$CellNumberPre."','".$CellNumber."','".$AlertOn."')";
    mysql_query($query);
    ?>
    PHP:
    This code validates if the field is empty, it also checks the CellNumber field if it's less than 7, and also checks if the email is valid...

    What I lack is, check if the CellNumber in the database already existed. Because I have found out that there are users that always register their cell number more than once, and it should not be allowed. I just want to ask PHP experts where can I insert in my code, a CellNumber validation if it already exists in my database. Just don't want a duplicated CellNumber. How Can I do that in my code? Without rewriting my code, just insert a simple duplicate validation thing... :(

    I hope someone kind enough out there will gladly help a newbie like me :(
     
    jehzlau, Jan 17, 2008 IP
  2. redvok

    redvok Active Member

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Place it before "if($errors == 1)"

    
    mysql_connect("localhost","jehzlau","password111");
    mysql_select_db("database");
    if($errors != 1)
    {
    $result = mysql_query("select count(1) from subscribers where cnp='$CellNumberPre' and cn='$CellNumber'");
    $row = mysql_fetch_row($result);
    if($row[0] > 0)
    {
    $errors=1;
    $error.="<p>Your already used this cellphone number </p>"
    }
    }
    
    Code (markup):
     
    redvok, Jan 17, 2008 IP
  3. junandya

    junandya Member

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #3
    maybe try this and put it after your $query

    $checkIfExist = mysql_num_rows($query)
    //if the result more than 0, it's mean that is already exist
    if ( $checkIfExist != 0 )
    {
     echo "<b>CellNumber already exist</b>";
    }
    PHP:
     
    junandya, Jan 17, 2008 IP
  4. jehzlau

    jehzlau Active Member

    Messages:
    301
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    73
    #4
    gotta try it now guys thanks :)
     
    jehzlau, Jan 17, 2008 IP
  5. jehzlau

    jehzlau Active Member

    Messages:
    301
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    73
    #5
    I encountered a syntax error after placing this before "if($errors == 1)"
    T___T
     
    jehzlau, Jan 17, 2008 IP
  6. jehzlau

    jehzlau Active Member

    Messages:
    301
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    73
    #6
    There was an unexpected Syntax error after IF, i don't know what's this error :(
     
    jehzlau, Jan 17, 2008 IP
  7. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #7
    Just a ; missing after $checkIfExist = mysql_num_rows($query)
    $checkIfExist = mysql_num_rows($query);

    :)
     
    wing, Jan 17, 2008 IP
  8. junandya

    junandya Member

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #8
    @wing: lol you right ...i forgot ;..thanx anyway
     
    junandya, Jan 17, 2008 IP
  9. jehzlau

    jehzlau Active Member

    Messages:
    301
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    73
    #9
    w8 gonna try this one.. heehee :)
     
    jehzlau, Jan 18, 2008 IP
  10. jehzlau

    jehzlau Active Member

    Messages:
    301
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    73
    #10
    what i did is this

    <?php
    
    $errors=0;
    $error="<b>The following errors occured while processing your form input.</b><br /><br />";
    
    $Name = $_POST['Name'];
    $Email = $_POST['Email'];
    $CellNumberPre = $_POST['CellNumberPre'];
    $CellNumber = $_POST['CellNumber'];
    $AlertOn = $_POST['AlertOn'];
    
    if($Name=="" || $Email=="" || $CellNumberPre=="" || $CellNumber=="" || $AlertOn=="" ){
    $errors=1;
    $error.="<p>You did not enter one or more of the required fields. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>";
    }
    
    if(!preg_match("/^\d+$/",$CellNumber)){
    $errors=1;
    $error.="<p>Only numbers are accepted in the cell number field. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>";
    }
    
    if (strlen($CellNumber) < 7){
    $errors=1;
    $error.="<p>Your cellphone number is not valid. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>";
    }
    
    if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$Email)){
    $error.="<p>Invalid email address entered. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>";
    $errors=1;
    }
    
    if($errors==1) echo $error;
    else{
    
    $link = mysql_connect("localhost","jehzlau","password111");
    mysql_select_db("database",$link);
    $query="insert into sms_subscribers (name,email,cnp,cn,alerton) values ('".$Name."','".$Email."','".$CellNumberPre."','".$CellNumber."','".$AlertOn."')";
    mysql_query($query);
    
    $checkIfExist = mysql_num_rows($query);
    //if the result more than 0, it's mean that is already exist
    if ( $checkIfExist != 0 )
    {
     echo "<b>CellNumber already exist</b>";
    }
    
    ?>
    PHP:

    and I encountered this error:

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/jehzwins/public_html/wp-sms/process.php on line 67

    :( that line 67 was

    $checkIfExist = mysql_num_rows($query);
    PHP:
    how can i fix this? :(
     
    jehzlau, Jan 18, 2008 IP
  11. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #11
    mysql_query() returns a statement handle. That's supposed to be the argument to mysql_num_rows().

    So...

    $st = mysql_query($query);
    $checkIfExist = mysql_num_rows($st);
    
    PHP:
     
    SmallPotatoes, Jan 18, 2008 IP
  12. jehzlau

    jehzlau Active Member

    Messages:
    301
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    73
    #12
    hi SmallPotatoes, where will i insert that code? :(

    i tried this:

    $st = mysql_query($query);
    $checkIfExist = mysql_num_rows($st);
    //if the result more than 0, it's mean that is already exist
    if ( $checkIfExist != 0 )
    {
     echo "<b>CellNumber already exist</b>";
    }
    PHP:
    but the same error still occured :(
     
    jehzlau, Jan 18, 2008 IP
  13. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Change this:

    mysql_query($query);
    
    $checkIfExist = mysql_num_rows($query);
    PHP:
    to this:

    $st = mysql_query($query);
    
    $checkIfExist = mysql_num_rows($st);
    PHP:
     
    SmallPotatoes, Jan 18, 2008 IP
  14. jehzlau

    jehzlau Active Member

    Messages:
    301
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    73
    #14
    Already tried it, but still encountered the same error :( where should I place this in my code? :(
     
    jehzlau, Jan 19, 2008 IP
  15. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #15
    "select count(1) from subscribers..."

    ...should be:

    "select count(1) from sms_subscribers..."
     
    SoKickIt, Jan 19, 2008 IP
  16. jehzlau

    jehzlau Active Member

    Messages:
    301
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    73
    #16
    The same error still occured.. :( I already changed subscribers to sms_subscribers :(

    Can you show me the complete code... because I'm so noob at PHP :(

    please... T_T
     
    jehzlau, Jan 20, 2008 IP
  17. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #17
    Why don't you post the complete code here?
     
    SoKickIt, Jan 20, 2008 IP
  18. jehzlau

    jehzlau Active Member

    Messages:
    301
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    73
    #18
    this is the complete code, it doesn't seem to work :(

    <?php
    
    $errors=0;
    $error="<b>The following errors occured while processing your form input.</b><br /><br />";
    
    $Name = $_POST['Name'];
    $Email = $_POST['Email'];
    $CellNumberPre = $_POST['CellNumberPre'];
    $CellNumber = $_POST['CellNumber'];
    $AlertOn = $_POST['AlertOn'];
    
    if($Name=="" || $Email=="" || $CellNumberPre=="" || $CellNumber=="" || $AlertOn=="" ){
    $errors=1;
    $error.="<p>You did not enter one or more of the required fields. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>";
    }
    
    if(!preg_match("/^\d+$/",$CellNumber)){
    $errors=1;
    $error.="<p>Only numbers are accepted in the cell number field. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>";
    }
    
    if (strlen($CellNumber) < 7){
    $errors=1;
    $error.="<p>Your cellphone number is not valid. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>";
    }
    
    if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$Email)){
    $error.="<p>Invalid email address entered. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>";
    $errors=1;
    }
    
    mysql_connect("localhost","jehzlau","password111");
    mysql_select_db("database");
    if($errors != 1)
    {
    $result = mysql_query("select count(1) from sms_subscribers where cnp='$CellNumberPre' and cn='$CellNumber'");
    $row = mysql_fetch_row($result);
    if($row[0] > 0)
    {
    $errors=1;
    $error.="<p>Your already used this cellphone number </p>"
    }
    }
    
    if($errors==1) echo $error;
    else{
    
    $link = mysql_connect("localhost","jehzlau","password111");
    mysql_select_db("database",$link);
    $query="insert into sms_subscribers (name,email,cnp,cn,alerton) values ('".$Name."','".$Email."','".$CellNumberPre."','".$CellNumber."','".$AlertOn."')";
    mysql_query($query);
    
    
    ?>
    PHP:
     
    jehzlau, Jan 20, 2008 IP
  19. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #19
    What kind of error do you get? You don't have "mysql_num_rows" in your code anymore.
     
    SoKickIt, Jan 20, 2008 IP
  20. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #20
    I see a missing ;.
    $error.="<p>Your already used this cellphone number </p>";

    And also a missing closing bracket:

    .... else{

    $link = mysql_connect("localhost","jehzlau","password111");
    mysql_select_db("database",$link);
    $query="insert into sms_subscribers (name,email,cnp,cn,alerton) values ('".$Name."','".$Email."','".$CellNumberPre."','".$CellNumber."','".$AlertOn."')";
    mysql_query($query);
    }
    :)
     
    wing, Jan 20, 2008 IP