www.webdeveloper.com
Results 1 to 4 of 4

Thread: PHP If Else Not working like i thought it would

  1. #1
    Join Date
    Aug 2009
    Posts
    6

    PHP If Else Not working like i thought it would

    Hello All I have the code below that i cant seem to get to work.

    At the bottom here you will find my code with a If Else statement. That i just cant get to do what i want. so i am hoping someone can help guide me in the right direction. I am gonna be honest i am VERY new to PHP so please bare with me.

    On the first If it checks to make sure that the promocode that was entered is in the database and that part works.

    On the second if i want it to look through the database and find the promocode and confirm that there is not an email address associated with that promocode record. Currently it is not doing that i can enter a promo code that has an email or not and it inserts into the DB.

    This is where my problem lies am i doing this completely wrong is there a better way to accomplish what i am trying to do here? Or have i just overlooked something small?
    PHP Code:
    $promosql "SELECT email IS NULL or email = '' has_email FROM formdata WHERE promoCode = '$varPromo'";
                
    $promoraw $mysqli->query($promosql);
                
                if(
    $promoraw->num_rows != 1) {
                    
    $promo .= "$varPromo is not a valid promocode \n";
                } else {
                    
    $row $promoraw->fetch_assoc();
                    if (
    $row['has_email']) {
                    
    // Promo code has been used
                    
    $dupe .= "$varPromo has already been used on $varDate \n";
                    } else {
                    
    // Insert into table
                    
    $sql "INSERT INTO formdata (promoCode, name, email, address, city, state, zip, submitDate) VALUES (".
                            
    PrepSQL($varPromo) . ", " .
                            
    PrepSQL($varName) . ", " .
                            
    PrepSQL($varEmail) . ", " .
                            
    PrepSQL($varAddress) . ", " .
                            
    PrepSQL($varCity) . ", " .
                            
    PrepSQL($varState) . ", " .
                            
    PrepSQL($varZip) . ", " .
                            
    PrepSQL($varDate) . ")";
                    
    $mysqli->query($sql);
                    
                    
    header("location: index.php?success=1");
                    exit();
                    }                    
                } 

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,408
    Is it possible for the same promo code to exist in the DB more than once? (I.e., does that column have a unique constraint on it or not?)

    If it could, then you might need to change that first if() to:
    PHP Code:
    if ($promoraw->num_rows == 0
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2009
    Posts
    6
    It isn't possible that there will be 2 of the same promo codes in the DB. We are manually loading those from a a file of unique ones.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,408
    Wait a sec....shouldn't the INSERT be an UPDATE?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles