PHP / SQL Multi INSERT INTO不起作用

时间:2016-03-05 06:21:17

标签: php jquery mysql sql insert

我无法一次插入2个标签。 它只插入其中一个(在本例中,它插入第一个)

   function addNewUser($username, $password, $email){
      $time = time();
      /* If admin sign up, give admin user level */
      if(strcasecmp($username, ADMIN_NAME) == 0){
         $ulevel = ADMIN_LEVEL;
      }else{
         $ulevel = USER_LEVEL;
      }
    $datumregistrationbla = date("d.m.Y");
      $q = "INSERT INTO ".TBL_USERS." (username, password, email, userlevel, register_date) VALUES ('$username', '$password', '$email', '$ulevel', '$datumregistrationbla')";
      return mysql_query($q, $this->connection);
      $q = "INSERT INTO `post` (`post_id`, `from`, `to`, `betreff`, `text`, `datum`, `active`) VALUES ('', 'Fuchsfeuer', '$username', 'Test', 'Test2', '$datumregistrationbla', '0')";
      return mysql_query($q, $this->connection);
   }

1 个答案:

答案 0 :(得分:2)

问题是因为你return第一个mysql_query而其余的不会被执行。请尝试将其删除。

function addNewUser($username, $password, $email){
      $time = time();
      /* If admin sign up, give admin user level */
      if(strcasecmp($username, ADMIN_NAME) == 0){
         $ulevel = ADMIN_LEVEL;
      }else{
         $ulevel = USER_LEVEL;
      }
    $datumregistrationbla = date("d.m.Y");
      $q = "INSERT INTO ".TBL_USERS." (username, password, email, userlevel, register_date) VALUES ('$username', '$password', '$email', '$ulevel', '$datumregistrationbla')";
      mysql_query($q, $this->connection);
      $q = "INSERT INTO `post` (`post_id`, `from`, `to`, `betreff`, `text`, `datum`, `active`) VALUES ('', 'Fuchsfeuer', '$username', 'Test', 'Test2', '$datumregistrationbla', '0')";
      mysql_query($q, $this->connection);
   }

请忘记使用mysql_*因为它已被弃用,而在php 7中它已被删除。
尝试使用mysqlipdo