IF or CASE statement inside INSERT

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ddtpmyra
    Contributor
    • Jun 2008
    • 333

    IF or CASE statement inside INSERT

    What is the syntax of inserting a value on a table with CASE statement or IF stmt.
    I have PHP form that holds the value, now I wanted to skip the insert value if the text box value is null but I dont know the correct syntax

    Code:
    "insert into table1 (id, category)
    values
    IF ($cat1 <> null) THEN
    ({$id}, '{$cat1}'), 
    ENDIF
    IF ($cat2 <> null) THEN
    ({$id}, '{$cat1}'), 
    ENDIF";

    OR

    Code:
     
    $query = "INSERT  
    WHEN $cat1 <> null then INTO table1 (Cal_ID,DateStamp,Category,Total) VALUES ({$id},NOW(),'{$cat1}',{$Totalcat1}) 
    WHEN $cat2 <> null then INTO table1  (Cal_ID,DateStamp,Category,Total) VALUES ({$id},NOW(),'{$cat2}',null);	";
    how can i do this correctly?
  • prabirchoudhury
    New Member
    • May 2009
    • 162

    #2
    1. check the submitted variables before executing the insert statement.

    2. if ($_POST[cat] !='null') { then execute the insert statement }

    3. or if ($cat !='null') { then execute the insert statement }

    4.
    Code:
        if ($cat1 !='null') { 
           $sql  = "INSERT INTO table1 (id, category) ";
                
          $sql .= " ({$id}, '{$cat1}')";
          mysql_query($sql);
          }

    :)

    Comment

    • ddtpmyra
      Contributor
      • Jun 2008
      • 333

      #3
      I have too many variables like 20 of them, and I wanted to put the insert query all together, is ther a way to make the if condition locally or after the insert statement?

      Originally posted by prabirchoudhury
      1. check the submitted variables before executing the insert statement.

      2. if ($_POST[cat] !='null') { then execute the insert statement }

      3. or if ($cat !='null') { then execute the insert statement }

      4.
      Code:
          if ($cat1 !='null') { 
             $sql  = "INSERT INTO table1 (id, category) ";
                  
            $sql .= " ({$id}, '{$cat1}')";
            mysql_query($sql);
            }

      :)

      Comment

      • prabirchoudhury
        New Member
        • May 2009
        • 162

        #4
        IF or CASE statement inside INSERT

        1. put your cat list in an array
        2. and then put this array in an for each loop , check the "null" values with if statement and then inside the if statement execute the sql

        Code:
        $cat_arr = array ();
        
        
        foreach ($arr as $key => $value) {
        
        if (check null){inser statement}
        
        }

        may be this would be helpful foreach

        Comment

        • ddtpmyra
          Contributor
          • Jun 2008
          • 333

          #5
          Grazie Prabirchoudhury (whew what a name)

          smile!
          Originally posted by prabirchoudhury
          1. put your cat list in an array
          2. and then put this array in an for each loop , check the "null" values with if statement and then inside the if statement execute the sql

          Code:
          $cat_arr = array ();
          
          
          foreach ($arr as $key => $value) {
          
          if (check null){inser statement}
          
          }

          may be this would be helpful foreach

          Comment

          • prabirchoudhury
            New Member
            • May 2009
            • 162

            #6
            thanks .. glad that works .. enjoy coding :)

            Comment

            Working...