2011-02-27 170 views
0

這需要添加多個行到數據庫,但它只是將數組中的第一個問題。有任何想法嗎?插入一個數組MySQL數據庫

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; 
      } 
     } 
    } 

回答

2

您正在循環中返回true。

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

return語句結束你的函數,併爲此你的​​循環,以及。所以,我會做這樣的事情:

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