将数组插入mysql数据库

时间:2011-02-27 09:38:21

标签: php mysql sql arrays multidimensional-array

这需要向数据库添加多行,但它只是在数组中添加第一个问题。有什么想法吗?

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

1 个答案:

答案 0 :(得分: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
}