Brandon
Brandon

Reputation: 13

Inserting an array into mysql database

This needs to be adding multiple rows to the database but it's only adding the first question in the array. Any ideas?

function addNewApp($question, $type, $username, $servername){  
      $time = time();  
      $q  = "INSERT INTO ".TBL_APPLICATIONS." VALUES ('0', '$username', '$servername', '', $time)";  
      if(mysql_query($q, $this->connection)){  
            return true;  
          }else{  
            return false;    
          }  
      for ($x=0; $x<count($question); $x++) {  
        $q2 = "INSERT INTO ".TBL_QUESTIONS." SET   `text`='".mysql_real_escape_string($question[$x])."', `id`='0', `servername`='$servername', `type`='$type[$x]'";  
          if(mysql_query($q2, $this->connection)){  
            return true;  
          }else{  
            return false;  
          }  
      }  
   }

Upvotes: 0

Views: 206

Answers (1)

underdoeg
underdoeg

Reputation: 1911

You are returning true within the loop.

if(mysql_query($q2, $this->connection)){
   return true;
}

The return statement ends your function and therefor your loop as well. So I'd do something like this:

if(!mysql_query($q2, $this->connection)){ //if correct, don't do anything
   echo "THERE WAS AN ERROR"; // or whatever sort of error reporting
}

Upvotes: 2

Related Questions