使用查询作为数组插入多个值

时间:2010-10-22 11:56:56

标签: php mysql

我正在尝试使用查询插入多个数据,我已尝试过implode函数,while循环,for循环,但仍然无法完成.. 你可以帮助PLZ

我有一个用于选择课程名称的组合框,创建了一个获取其ID并分配变量的函数。我是一个部门的经理,需要为我下面的所有员工分配一门课程,我选择课程,输入分配的日期和预期的结束日期。我在数据库中创建了另一个字段以进入培训所有者。由于我是1分配课程,我的名字将显示为所有者字段。

$m_name = $_SESSION['SESS_FIRST_NAME'];
//combobox to get the department ID using variable $dept
//query to get all user concerning the department
$query  = mysql_query("select userid from dept_user where dept_id=$dept LIMIT 0, 30 ");
$row= mysql_query($query);

//from here i'm not being able to execute
 $qry = mysql_query("INSERT INTO course_detail(userid, course_id, date_assign, expected_end_date, owner) VALUES('$query','$name','$sdate', '$edate', '$m_name')" ) ;

5 个答案:

答案 0 :(得分:1)

让我们猜测一下,假设这就是你想要的:

//query to get all user concerning the department
$query  = mysql_query("
    SELECT userid 
    FROM dept_user 
    WHERE dept_id=$dept 
");
if(mysql_num_rows($query)){
    $insertSQL = "
        INSERT INTO course_detail 
        (userid, course_id, date_assign, expected_end_date, owner) 
        VALUES
    ";
    $rowsSQL = Array();
    while($row = mysql_fetch_row($query)){
        $rowsSQL[] = "('{$row['userid']}','$name','$sdate', '$edate', '$m_name')";
    }
    mysql_query($insertSQL.implode(',', $rowsSQL));
}

此外,您应该开始阅读the manual

答案 1 :(得分:1)

$qry = mysql_query("INSERT INTO course_detail(userid, course_id, date_assign, expected_end_date, owner) VALUES('$query','$name','$sdate', '$edate', '$m_name')" ) ;

因此,您基本上尝试在userid列中插入$ query。在您的代码中,$ query是mysql select语句的结果,因此是一个多用户ID数组。可以把它想象成一个简单的SQL查询,你无法执行它。更重要的是,你在mysql_query结果上做了mysql_query,这是完全错误的。 $ dept变量来自哪里?其他人怎么样?如果你确定它们在这里是有效的,那就是你需要的:

// Get the user ids you need to insert in the db
$query = "select userid from dept_user where dept_id=$dept LIMIT 0, 30 "; // this will select the first 30 users in a dept
$buffer = mysql_query($query); // this is a variable that will hold all the results returned by the query above

// While we still have results in the $buffer array, fetch those in the $data array
while ($data = mysql_fetch_assoc($buffer)) {
    $insert_query = "INSERT INTO course_detail(userid, course_id, date_assign, expected_end_date, owner) VALUES('".$data['userid']."','$name','$sdate', '$edate', '$m_name')"; // add the userid from the first query and the other data (don't know where you got those
    $insert_buffer = mysql_query($insert_query); // execute the statement above, watch out so you don't overwrite the initial $buffer variable
}
// At this point you should have all the data in database

另外,我不确定你的插入声明是否正确

  • userid> $ data ['userid'](ok)
  • course_id> $ name(?!)
  • date_assign> $ sdate(你确定吗?)
  • expected_end_date> $ edate(ok)
  • 所有者> $ m_name(好吗?)

确保你有一个很好的命名惯例,否则你很容易迷失。

祝你好运,只需5行代码就会出现很多错误。

答案 2 :(得分:0)

阅读the doc,您可以用逗号分隔每组数据。

答案 3 :(得分:0)

$values = array();    
$values[] = array(
  'id'  => 1,
   'v1' => 'a',
   'v2' => 'b'
);
$values[] = array(
  'id'  => 2,
   'v1' => 'c',
   'v2' => 'd'
);

$sql = "INSERT INTO course_details (id,v1,v2) VALUES ";
foreach ($values as $value) {
  $sql .= '('.$value['id'].','.$value['v1'].','.$value['v2'].'),';
}
$sql = substr ($sql,0,-1) // that will remove the last comma
mysql_query($sql);

答案 4 :(得分:0)

以下是该功能,以下是如何使用它

  

更新查询

    $data_array=array(
        "bannername" => addslashes($_POST["bannername"]),
        "url" => $_POST["url"],
        "openin" => $_POST["openin"]
    );

    $param=" bannerid = '$bannerid'";
    $sql=db_makequery("banner",$data_array,"update",$param);  
  

插入查询

    $data_array=array(
        "bannername" => addslashes($_POST["bannername"]),
        "url" => $_POST["url"],
        "openin" => $_POST["openin"]
    );

$sql=db_makequery("banner",$data_array);  
  

<强>功能

function db_makequery($table, $data, $action = 'insert', $parameters = '') 
    {
        reset($data);

        if ($action == 'insert') 
        {
          $query = 'insert into ' . $table . ' (';
          while (list($columns, ) = each($data)) {
            $query .= $columns . ', ';

          }

          $query = substr($query, 0, -2) . ') values (';

          reset($data);
          while (list(, $value) = each($data)) 
          {
            switch ((string)$value) 
            {
              case 'now()':
                $query .= 'now(), ';
                break;
              case 'null':
                $query .= 'null, ';
                break;
              default:
                //$query .= '\'' . tep_db_input($value) . '\', ';
                $query .= '\'' . $value . '\', ';

                break;
            }
          }
          $query = substr($query, 0, -2) . ')';
        }
        elseif ($action == 'update') 
        {
          $query = 'update ' . $table . ' set ';
          while (list($columns, $value) = each($data)) 
          {
            switch ((string)$value) {
              case 'now()':
                $query .= $columns . ' = now(), ';
                break;
              case 'null':
                $query .= $columns .= ' = null, ';
                break;
              default:
                //$query .= $columns . ' = \'' . tep_db_input($value) . '\', ';
                $query .= $columns . ' = \'' . $value . '\', ';
                break;
            }
          }
          $query = substr($query, 0, -2) . ' where ' . $parameters;
        }
        return $query; 
      }