2010-10-22 69 views
0

我試圖插入多個數據使用查詢,我已經試過了內爆函數,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')") ; 
+1

歡迎SO。由於您沒有提供足夠的信息,因此您的問題目前無法解答。請告訴我們「多個數據」的格式。顯示一個例子。 – 2010-10-22 11:59:52

+1

你想用'$ row = mysql_query($ query)來實現什麼;'?您可能需要這裏的一個'mysql_fetch_ *'函數。 – 2010-10-22 12:02:08

回答

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是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 

而且,我不知道你得到了INSERT語句正確

  • 用戶ID> $數據[「用戶ID」( OK)
  • COURSE_ID> $名(?!)
  • date_assign> $ SDATE(你確定嗎?)
  • expected_end_date> $ EDATE(OK)
  • 所有者> $ m_name(OK?)

確保你有一個好的命名約定,否則你很容易迷路。

祝你好運,在5行代碼上犯了很多錯誤。

+0

thx dr ...其工作.. gr8 .. txk很多很多幫助我 – vimal 2010-10-22 12:58:32

+0

任何時候,和其他人一樣,閱讀手冊,看​​起來像生產的東西給我(用戶,部門...) – Claudiu 2010-10-22 13:12:46

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

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