Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts

    testing for blank variables ?

    Hello

    I want to insert values only if the variables are not blank.

    Is this the correct way to test it (in the if statement) ?

    PHP Code:
    if(!$N_contact1 == "" && !$N_email1 == ""){          
            
     
    $sql "INSERT INTO freinds (f_name, f_email, up_name, up_email, create_date) 
     VALUES ('$N_contact1','$N_email1','$N_contact','$N_email','$today')"
    ;

     
    mysql_query($sql)
     or die(
    "could not execute CLIENT INSERT query");
    // end if 
    Thanks for any advice.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    PHP Code:
    if($N_contact1 !== "" && $N_email1 !== ""){ 

  • #3
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    PHP Code:
    if(!empty($N_contact1) && !empty($N_email1)) {
     
    $sql "INSERT INTO freinds (f_name, f_email, up_name, up_email, create_date) 
     VALUES ('$N_contact1','$N_email1','$N_contact','$N_email','$today')"
    ;

     
    mysql_query($sql)
     or die(
    "could not execute CLIENT INSERT query");
    // end if 
    this?
    www.php.net/empty

  • #4
    Regular Coder
    Join Date
    Sep 2007
    Location
    Grahamstown, South Africa
    Posts
    237
    Thanks
    6
    Thanked 17 Times in 17 Posts
    the trim issue with empty() gets me a little annoyed sometimes... what follows is what you need.
    PHP Code:
    <?php
    if (empty($var)){
      echo(
    "Empty");
    } else {
      echo(
    "Full");
    }
    ?>
    hmmm, a little unrelated but does anyone know if you can run trim on a variable before using the empty statement?
    Last edited by idalatob; 02-25-2008 at 10:03 AM. Reason: added php thingamabobs

  • #5
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Thanks for your replies.

    Is there any difference in the results of:


    1) if($N_contact1 !== "" && $N_email1 !== ""){

    and

    2) if(!empty($N_contact1) && !empty($N_email1)) {

    ???
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by jeddi View Post
    Thanks for your replies.

    Is there any difference in the results of:


    1) if($N_contact1 !== "" && $N_email1 !== ""){

    and

    2) if(!empty($N_contact1) && !empty($N_email1)) {

    ???
    Yes, if someone enters (0, "0", null, false, array()) it will be considered empty(), but not an empty string(!== "")


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •