tags:

views:

45

answers:

5

Hello, i'm trying to insert multiple data using a query, i've tried the implode function, the while loop, for loop, but still can't be done.. can u help plz

well i've a combobox box for selecting course name, created a function to get its ID and assign a variable. supose i'm a manager of a department and need to assign all staff below me a course, i select the course, input the date assigned and expected ending date. i've created another field in database to enter the training owner. Since i'm the 1 assigning the course, my name will appear as owner field.

$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')" ) ;
A: 

Read the doc, you can separate each set of data with a comma.

MatTheCat
A: 
$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);
mimrock
A: 

Let's give it a wild guess and assume that this is what you want:

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

Also you should start reading the manual.

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

So, you're basically trying to insert $query in the userid column. In your code, $query is the result of a mysql select statement, thus a multi-array of user ids. Think of it like a simple SQL query, you can't execute that. Even more, you're doing mysql_query on a mysql_query result, which is plain wrong. Where does the $dept variable come from? What about the others? If you're sure they're valid here's what you need:

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

Also, I'm not sure you got the insert statement right

  • userid > $data['userid'] (ok)
  • course_id > $name (?!)
  • date_assign > $sdate (you sure?)
  • expected_end_date > $edate (ok)
  • owner > $m_name (ok?)

Make sure you have a good naming convention or else you get lost very easy.

Good luck, a lot of mistakes on just 5 lines of code.

Claudiu
thx dr... its working.. gr8.. txk a lot alot for helping me dr
vimal
Any time, and as the other suggested, read the manual, looks like production stuff to me (users, departments...)
Claudiu
A: 

Below is the function and here is how to use it

Update Query

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

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

Insert Query

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

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

Function

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; 
      }
gajendra.bang