【问题标题】:Inserting an array into mysql database将数组插入mysql数据库
【发布时间】:2011-07-05 04:23:57
【问题描述】:

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

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

【问题讨论】:

    标签: php mysql sql arrays multidimensional-array


    【解决方案1】:

    您在循环中返回 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
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-25
      • 2014-07-21
      • 2014-04-26
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      • 2016-05-26
      • 2017-07-03
      相关资源
      最近更新 更多