Hello, i'm trying to insert multiplle values into a table.. but it is being stored o in the table.. i've been trying while loop but still no progress.. the system search for user related to its specefic staff concern and then assign a course..

<form id="assign" name="assign" method="post" action="assign_training.php">

    <tr><td class="style3">Select course name:</td>
    <td><select name="name" class="principal2"><?php select_coursename(); ?></select></td></tr>
    <tr>
    <td class="style3">Select Department:</td>
    <td><select name="dept" class="principal2"><?php select_dept(); ?></select></td>
    </tr>
    <tr><td class="style3"> Start Date</td>
<td><input type="date" name="sdate"></td></tr>
<tr><td class="style3"> End Date</td>
<td><input type="date" name="edate"></td></tr>
    <tr>
      <div align="right"class="lien">
        <td><input type="submit" name="submit" value="assign to department"  />
        <td><input type="submit" name="submit" value="assign to individual"  />
        </div></td>    
    </tr></form></table>

assigntraining.php

<?php
include('include.php'); 
    include('config.php');
    require_once('auth.php');
    session_start();



    //Function to sanitize values received from the form. Prevents SQL injection
    function clean($str) {
        $str = @trim($str);
        if(get_magic_quotes_gpc()) {
            $str = stripslashes($str);
        }
        return mysql_real_escape_string($str);
    }

    //Sanitize the POST values
    $name = clean($_POST['name']);
    $dept= clean($_POST['dept']);
    $date = clean($_POST['sdate']);
    $edate = clean($_POST['edate']);



    //Input Validations
    if($name == '') {
        $errmsg_arr[] = 'Select Course Name';
        $errflag = true;
    }
    if($dept == '') {
        $errmsg_arr[] = 'Select Department';
        $errflag = true;
    }
    if($date == '') {
        $errmsg_arr[] = 'Enter assign date';
        $errflag = true;
    }


    /*
    //If there are input validations, redirect back to the registration form
    if($errflag) {
        $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
        session_write_close();
        header("location: register-form.php");
        exit();
    }
*/
    $m_name = $_SESSION['SESS_FIRST_NAME'];
    $r = mysql_query("select userid from dept_user where dept_id=$dept  LIMIT 0, 30 ");

        {
    $qry = "INSERT INTO course_detail(userid, course_id, date_assign, expected_end_date, owner) VALUES('[$r]','$name','$sdate', '$edate', '$m_name')"  ;

    }$result = @mysql_query($qry);

//Check whether the query was successful or not
    if($result) {
        header("location: assigntraining.php");
        exit();
    }else {
        die("Query failed");
    }
?>

after this is executed:

$r = mysql_query("select userid from dept_user where dept_id=$dept LIMIT 0, 30 ");

$r does NOT have the userid. It "points" to "something" (a resource) which contains the actual data from the result set (assuming some result was actually returned). What you need to do is EXTRACT the column value from that resource:

...
$r = mysql_query("select userid from dept_user where dept_id=$dept LIMIT 0, 30 ") or die( mysql_error() );

//here you "Extract" the data from the resource
while( $row=mysql_fetch_assoc($r) );
{
$qry = "INSERT INTO course_detail(userid, course_id, date_assign, expected_end_date, owner) VALUES('" . $row['userid'] . "','$name','$sdate', '$edate', '$m_name')" ;
  echo 'executing '.$qry.'<br/>';
  mysql_query($qry) or die( mysql_error() );

};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.